Add CheckBoxList checked (selected) Item to DropDownList using C# and VB.Net in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

Hello, 

I try to add list items to a dropdown list box from a checklist web control, how can I select the listitems from the dropdown list and link to other page on the current website.

Please help. Thank you.

Here are my codes:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
        <asp:ListItem>add1</asp:ListItem>
        <asp:ListItem>add1</asp:ListItem>
        <asp:ListItem>add1</asp:ListItem>
        <asp:ListItem>add1</asp:ListItem>
    </asp:CheckBoxList>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</asp:Content>

C#:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)        
{
    DropDownList1.Items.Clear();
    foreach (ListItem li in CheckBoxList1.Items)            
    {                
        if (li.Selected)                
        {
            DropDownList1.Items.Add(li.Text); 
            if (DropDownList1.Selectedvalue=="1")
            { 
                Response.Redirect("next.aspx");                
            }
        }        
    }
}

 

Posted 2 months ago

Hi KatieNgoc,

Refer below sample.

HTML

<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
    <asp:ListItem Value="1">add1</asp:ListItem>
    <asp:ListItem Value="2">add2</asp:ListItem>
    <asp:ListItem Value="3">add3</asp:ListItem>
    <asp:ListItem Value="4">add4</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="Redirect">
</asp:DropDownList>

Namespaces

C#

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList1.Items.Clear();
    DropDownList1.Items.Insert(0, new ListItem("Select", "0"));
    foreach (ListItem li in CheckBoxList1.Items)
    {
        if (li.Selected)
        {
            DropDownList1.Items.Add(new ListItem(li.Text, li.Value));
        }
    }
}

protected void Redirect(object sender, EventArgs e)
{
    if (DropDownList1.SelectedValue == "1")
    {
        Response.Redirect("next.aspx");
    }
}

VB.Net

Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    DropDownList1.Items.Clear()
    DropDownList1.Items.Insert(0, New ListItem("Select", "0"))
    For Each li As ListItem In CheckBoxList1.Items
        If li.Selected Then
            DropDownList1.Items.Add(New ListItem(li.Text, li.Value))
        End If
    Next
End Sub

Protected Sub Redirect(ByVal sender As Object, ByVal e As EventArgs)
    If DropDownList1.SelectedValue = "1" Then
        Response.Redirect("next.aspx")
    End If
End Sub

Screenshot