Uncategorized

Create a Custom GroupBox for selecting multiple choice in ASP.Net and C#

Many a time , We need to create a control for selecting Multiple Inputs in our Web Applications .

I am gonna show a demo how we can create a GroupBox type thing to select multiple items and Get those values in server side .

Lets see an example …

First see the image below :

Custom GroupBox for selecting multiple choice in ASP.Net and C#

Custom GroupBox for selecting multiple choice in ASP.Net and C#

If our requirement is like – User wants to see the report for all the Tasks which are in Open and Close Status.
So User can easily select multiple options .

So Here is my mark up code below :::

<asp:GridView ID="gridTicketStatus" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:CheckBox ID="selectCheckBox" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Id" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblStatusId" runat="server" Text='<%#Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Select Status">
                        <ItemTemplate>
                            <asp:Label ID="lblTaskStatus" runat="server" Text='<%#Bind("TaskStatus") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Here I used a ASP.Net Grid which gets the status from the Database and Binds the Status.And I used another CheckBox , that’s how we can create any custom control . If we also need any DropDown inside it, we can easily add the DropDown with it as same way .

The Grid binding code in C# is like :::

DataTable dtStatus = TaskStatusGateway.GetAllTaskStatuses();
            gridTicketStatus.DataSource = dtStatus;
            gridTicketStatus.DataBind();

And after clicking the Button we can easily get all the selected Statuses by doing like below.

List<int> checkedStatus = new List<int>();

            if (gridTicketStatus.Rows.Count > 0)
            {
                for (int i = 0; i < gridTicketStatus.Rows.Count; i++)
                {
                    if (((CheckBox) (gridTicketStatus.Rows[i].FindControl("selectCheckBox"))).Checked)
                    {
                        int id = Convert.ToInt32(((Label) gridTicketStatus.Rows[i].FindControl("lblStatusId")).Text);
                        checkedStatus.Add(id);
                    }
                }
            }

So we are done .

Cheers 🙂

Standard