Contents tagged with listview

  • ListViewUpdateEventArgs, Item and CheckBox Weirdness

    I went around in circles on these issues, so maybe this post will help someone else. I still haven't got a handle on what's going on. Maybe a wise and knowledgeable reader will add a comment and flesh this out. If it makes a difference, I'm using the ObjectDataSource.

    My goal was to get the value of a checkbox control inside a ListView

    <EditItemTemplate>. For some strange reason, the compiler complains about Bind() in the declarative markup for certain controls but not others. For those, the best you can get in the markup is Eval(), used like this:

    <asp:CheckBox ID="chkIS_DEFAULT" runat="server" 
    Checked='<%# Convert.ToBoolean(Eval("IS_DEFAULT")) %>' />
    

    This means you have to bind the control using code, which is pretty weird as well. But what's even more bizarre is the way you need to jam in the value within the ListView control's ItemUpdating event:

    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        // To get the checkbox value (can't be done declaratively) --kc
        CheckBox chk;
        chk = ListView1.Items[e.ItemIndex].FindControl("chkIS_DEFAULT") as CheckBox;
        if (chk != null)
        {
            e.NewValues["IS_DEFAULT"] = chk.Checked.ToString().ToUpper();
        }
    }

    The ListViewUpdateEventArgs class is really different from its insertion counterpart, ListViewInsertEventArgs. Within the <InsertItemTemplate>, I was able to do it like this:

    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        
        CheckBox chk = e.Item.FindControl("chkIS_DEFAULT") as CheckBox;
        if (chk != null)
        {
            e.Values["IS_DEFAULT"] = chk.Checked.ToString().ToUpper();
        }
    }

    This is all quite baffling. I'd like to blame in on C# idiocy (I'm a VB fan) but I'm sure there's a good explanation (bug??) for the nonsense.

    Just for good measure, here's what I had to do to get the value from a dropdownlist control:

    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        
        DropDownList ddl2 = e.Item.FindControl("ddlSTATUS") as DropDownList;
        if (ddl != null)
        {
            e.Values["STATUS"] = ddl2.SelectedValue.ToString();
        }
    
    }

    What's up with all this?

    Ken

    MVP [ASP.NET]

     

    Read more...

  • Speed Test: A No-Code LINQ-Based ASP.NET 3.5 Page

    How fast can you create a single-page, coding-free, AJAX-enhanced, Linq-driven Web application from scratch in VS 2008v2/VWD? Very fast, I'll bet.

    I've prepared these instructional steps. Your challenge is to follow them closely and report back how many minutes/seconds it took. You only get one shot (after that you're not a newbie).

    Please report any errors, bugs, missing steps, troublespots, or ways to make this shorter. The process bogs somewhat in the Create a Database section but mainly suffers in the Configure the ListView Columns section. (I'm not sure why the column alignment goes out of whack and needs repair.)

    Anyway, have fun and let me know?

    Prepare the Project

    1. In VS 2008b2, create a new Web site (File > New Web Site > ASP.NET Web Site).
    2. Delete the Default.aspx that uses code-behind.
    3. Add a new page called Default.aspx (File > New File > Web Form > (Uncheck Place code in separate file) > Add).

    Create the Database

    1. Add a database into the App_Data folder (File > New File > SQL Database > Add).
    2. In Server Explorer, select the Tables node (Data Connections > Database.mdf > Tables)
    3. Add a new table (Data > Add New > Table).
    4. Add an ID column. (Column name [id] > Data Type [int] > Identity Specification > (Is Identity) > Yes).
    5. Set ID as the primary key. (Table Designer > Set Primary Key)
    6. Add a Title column. (Column name [Title] > Data Type [varchar(50)])
    7. Add a Description column. (Column name [Description] > Data Type [varchar(300)])
    8. Save the Table Designer (File > Save Table1 > OK).
    9. Close the Table Designer.

    Create a LINQ to SQL Class and DataContext

    1. In Solution Explorer, add a LINQ to SQL Class to the App_Code folder (File > New File > LINQ to SQL Classes > OK)
    2. If DataClasses.dbml isn't already open, open it.
    3. Switch to Server Explorer.
    4. Drag Table1 from Server Explorer and drop it on the left-hand pane (the Object Relational Designer).
    5. Save DataClasses.dbml and close the designer.

    Configure the LinqDataSource

    1. Open the ASP.NET page in Design view.
    2. From the Toolbox, drag a LinqDataSource control and drop it on the page.
    3. In the LinqDataSource Smart Tasks, click Configure Data Source.
    4. Choose DataClassesDataContext as the context object and click Next.
    5. Click Finish.
    6. On the LinqDataSource Smart Tasks, select Enable Delete, Enable Insert, and Enable Update.

    Configure the ListView

    1. From the Toolbox, drag a ListView control and drop it on the page.
    2. From the Smart Tasks, choose the data source LinqDataSource1.
    3. From the Smart Tasks, click Configure ListView.
    4. Select Grid, Colorful, and enable Editing, Inserting, Deleting, and Paging.
    5. Click OK.

    Configure the ListView Columns

    1. From the ListView's Smart Tasks, set the Current View to AlternatingItemTemplate.
    2. In Design view, put your mouse in the ID column header.
    3. Right-click, and from the context menu click Delete > Delete Columns.
    4. From the Smart Tasks, set the Current View to EditItemTemplate.
    5. In the Title column, delete idLabel1.
    6. Move the two Textbox controls one column to the left.
    7. From the Smart Tasks, set the Current View to ItemTemplate.
    8. In the Title column, delete idLabel.
    9. Move TitleLabel and DescriptionLabel one column to the left.
    10. From the Smart Tasks, select SelectedItemTemplate.
    11. From the Title column, delete idLabel.
    12. Move TitleLabel and DescriptionLabel one column to the left.

    Configure ASP.NET AJAX

    1. From the Toolbox, drag an AJAX ScriptManager control and drop it above the ListView control.
    2. Drag an UpdatePanel and drop it to the right of the ListView control.
    3. Select the ListView control and drop it inside the Update Panel.

    Run and Test the Page

    1. Browse to the page.
    2. Add a title and description.
    3. Click Insert.

    Hey, playing with this new stuff sure beats working for a living!

     

    Read more...