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(); } }
Ken
MVP [ASP.NET]