Biswarup Saha

Biswarup Saha

  • NA
  • 58
  • 31.3k

How to get checked items from dynamically created checkbox

Dec 4 2013 12:31 AM
Here is my code through which i can generate dynamically 2 checkbox group
 
DataTable dt = new DataTable();
DataList1.DataSource = dt;
DataList1.DataBind();
DataList2.DataSource = dt;
DataList2.DataBind();
 
Design Page
 
<asp:DataList runat="server" ID="DataList1" RepeatDirection="Vertical">
        <ItemTemplate>
        <table width="290px">
            <tr>
                <td align="left"><asp:CheckBox ID="chkBttn" CssClass="chck_btn" ToolTip='<%# Eval("Price")%>' Text='<%# Eval("OptionalServicesName")%>' Checked="false" runat="server" OnCheckedChanged="CheckBox_CheckedChanged" AutoPostBack="true" /></td>
                <td align="right" width="40px"><asp:Label ID="ExtraPrice" Text='<%# Eval("Price")%>' runat="server"></asp:Label> </td>
            </tr>
        </table>
 
        </ItemTemplate>
        </asp:DataList>
<asp:DataList runat="server" ID="DataList2" RepeatDirection="Vertical">
        <ItemTemplate>
        <table width="290px">
            <tr>
                <td align="left"><asp:CheckBox ID="chkBttn" CssClass="chck_btn" ToolTip='<%# Eval("Price")%>' Text='<%# Eval("OptionalServicesName")%>' Checked="false" runat="server" OnCheckedChanged="CheckBox_CheckedChanged" AutoPostBack="true" /></td>
                <td align="right" width="40px"><asp:Label ID="ExtraPrice" Text='<%# Eval("Price")%>' runat="server"></asp:Label> </td>
            </tr>
        </table>
        
        </ItemTemplate>
        </asp:DataList>
 
There is two checkbox group which have same datasource, so in both case same checkbox are showing and also same event is firing for both. Here is the event code
 
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
string OpService = ((CheckBox)sender).Text;
}
 

Now what i want to do is, while check one checkbox from the 1st checkbox group the same checkbox should be selected from the second checkbox group automatically, also if i deselect one checkbox that should be deselect from both checkbox group.
 
Please guide me to solve this.
Thanks in advance.