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]

 

3 Comments

  • Maybe I'm up against this?

    An ASP.NET 2.0 Web application does not send actual values to the parameters of the Update method of the data source when you use data binding to update data in a templated control

    http://support.microsoft.com/kb/941155

  • Ken,

    Were you trying to do something like this?

    <asp:CheckBox ID="chkIS_DEFAULT" runat="server"
    Checked='' />

    If so, it is important to understand that "Bind" in declarative syntax is not a function call. It is a special keyword that tells the compiler to generate code for two way databinding (one to set the value and the other to extract the value).
    The only way it can be used is like so:



    Can the resultset from the ObjectDataSource return a boolean for IS_DEFAULT?

    If not, you could write a custom CheckBox control that does the conversion to boolean internally which allows the compiler to generate the two way databinding code.

    Another option would be to add event handlers for the ObjectDataSource Updating, Inserting events and set/replace the value(s) in code behind.

    Raj Kaimal

  • Raj:

    Interesting. The database uses a string for what would normally be a Boolean (I'm not the DBA). However, my middle tier can probably do the conversion to make this cleaner on the ASP.NET side.

    Thanks!

    Ken

Comments have been disabled for this content.