Popular Posts

Nov 17, 2013

Dynamic Checkbox in ASP.NET (Return checkbox value in comma seperated format)




In .aspx File:

<asp:Label ID="lblSelectedValues" runat="server" Text="" style="color: #FF3300"/>

<asp:CheckBoxList ID="AccCheckBoxList" runat="server" RepeatColumns="2" Height="55px" Width="239px" RepeatLayout="Table" BorderColor="Red" BorderStyle="Dotted" BorderWidth="4px">
           </asp:CheckBoxList>


<asp:CheckBoxList ID="AccCheckBoxList" runat="server" RepeatColumns="2" Height="55px" Width="239px" RepeatLayout="Table" BorderColor="Red" BorderStyle="Dotted" BorderWidth="4px">

           </asp:CheckBoxList>



In .aspx.cs File:

private void GetAccListCheckBox()
    {
        DataTable dt = new DataTable();
        dt = empManagerObj.GetAccList();
        AccCheckBoxList.DataSource = dt;
        AccCheckBoxList.DataTextField = "accessories_name";
        AccCheckBoxList.DataValueField = "id";
        AccCheckBoxList.DataBind();
    }


    protected void BtnGetSelectedValues_Click(object sender, EventArgs e)
    {
        if (AccCheckBoxList.SelectedIndex != -1)
        {
            lblSelectedValues.Text = "Selected values are = " + GetCheckBoxListSelections();
        }
        else
        {
            lblSelectedValues.Text = "Please select any course";
        }
    }

    private string GetCheckBoxListSelections()
    {
        string[] cblItems;
        ArrayList cblSelections = new ArrayList();
        foreach (ListItem item in AccCheckBoxList.Items)
        {
            if (item.Selected)
            {
                cblSelections.Add(item.Value);
            }
        }

        cblItems = (string[])cblSelections.ToArray(typeof(string));
        return string.Join(",", cblItems);
    }

    protected void btnClearSelection_Click(object sender, EventArgs e)
    {
        AccCheckBoxList.ClearSelection();
        lblSelectedValues.Text = string.Empty;
    }



No comments:

Post a Comment