Passing Value to an OnCheckedChanged Event Handler of asp.net Checkbox Control
Scenario
In one of our project we have a custom CompositeDataBoundControl. I am binding the control with my custom objects collection and displaying records in <li>.
Each record has One of the <li> that is hidden and contains the UniqueID to identify the record and another <li> has a checkbox that will display boolean value. Now I wanted to update the record with true or false when I check / uncheck the checkbox in my custom control.
Below is my sample control which is to demonstrate the scenario. So if you just copy-paste the code its not going to work. As the purpose is to focus on checkbox and passing value.
<cc1:MyCustomControl Width="100%" runat="server" ID="MyCustomControl1" itemPlaceHolder="itemPlaceHolder1" ><
LayoutTemplate><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></
LayoutTemplate><HeaderTemplate> <div > <ul><li >First Name</li>
<li >Last Name</li> <li >Active</li></ul></
div></HeaderTemplate><
CustomerControlTemplate> <div > <ul > <li > <%# Container.DataItem.FirstName %></li> <li > <%#Container.DataItem.LastName %></li> <li> <asp:CheckBox ID="chbActive" OnCheckedChanged="chbActive_OnCheckedChanged" AutoPostBack="true" runat="server" Checked="<%# Container.DataItem.Active %>" /> </li> <li class="hiddenitem" runat="server" id="CustID" style="visibility:hidden;"><%# Container.DataItem.CustID %></li> </ul> </div> </CustomerControlTemplate><EmptyDataTemplate><
span>No Data Available.</span></EmptyDataTemplate></
cc1:MyCustomControl >Now what we want is when I Check or UnCheck then chbActive the corresponding record should be updated in the database. For this to happen I must get the CustID and pass chbActive.Checked to the Database.
Below is the code for chbActive_OnCheckedChanged which is the main part you might be looking for.
protected void cbbActive_OnCheckedChanged(object sender, EventArgs e){
CheckBox chbActive = (CheckBox)sender; HtmlGenericControl li = (HtmlGenericControl)chbActive.NamingContainer.FindControl("CustID");int CustID = Convert.ToInt32(li.InnerText.ToString()); bool active = chbActive.Checked;
//Now you have two important values
//Pass it to your update query or as a parameters to your stored Proc to update the corresponding record.
}
The core of this code is:
CheckBox chbActive = (CheckBox)sender; HtmlGenericControl li = (HtmlGenericControl)chbActive.NamingContainer.FindControl("CustID");int CustID = Convert.ToInt32(li.InnerText.ToString());
-- Get Reference to the CheckBox
-- Get reference to the <li> that contains the CustID. In the mark-up see we have an ID and runat="server" for your hidden <li>.
-- Get the InnerText of the CustID <li>.
Also worth noting in mark-up:
<li> <asp:CheckBox ID="chbActive" OnCheckedChanged="chbActive_OnCheckedChanged" AutoPostBack="true" runat="server" Checked="<%# Container.DataItem.Active %>" /> </li>
--- AutoPostBack is set to true.
<li runat="server" id="CustID" style="visibility:hidden;" ><%# Container.DataItem.CustID %></li>--- runat="server" and id attributes are set
I know I did not explain a lot but hope the code will speak for itself.
Thanks to the following resources that helped me find answer to my problem.
References: