Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

Any body help in code of Drop Down Check List Box that drop Down list retrieve from database and then after save into database it.

Aspx Code :
ASP.NET
<asp:DropDownCheckBoxes ID="ddchkCountry" runat="server" AddJQueryReference="True" UseButtons="True" UseSelectAllNode="True">
        <Style SelectBoxWidth="200" DropDownBoxBoxWidth="200" DropDownBoxBoxHeight="130" />
        <Texts SelectBoxCaption="Select Country" /></asp:DropDownCheckBoxes>
   
        <asp:Label ID="lblCountryID" runat="server"></asp:Label><br />
        <asp:Label ID="lblCountryName" runat="server"></asp:Label>

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>

C# Code :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DropDownList19_12
{
    public partial class Register : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=DLITTLES-PC;Database=MultiCheck;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        { 
            DataSet ds = new DataSet();
            string cmdstr = "select CountryID,Country from Country";
            SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
            adp.Fill(ds);
           
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddchkCountry.DataSource = ds.Tables[0];
                ddchkCountry.DataTextField = "Country";
                ddchkCountry.DataValueField = "CountryID";
                ddchkCountry.DataBind();
            } 
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<string> CountryID_list = new List<string>();
            List<string> CountryName_list = new List<string>();

            foreach (System.Web.UI.WebControls.ListItem item in ddchkCountry.Items)
            {
                if (item.Selected)
                {
                    CountryID_list.Add(item.Value);
                    CountryName_list.Add(item.Text);
                }

                lblCountryID.Text = "Country ID: " + String.Join(",", CountryID_list.ToArray());
                lblCountryName.Text = "Country Name: " + String.Join(",", CountryName_list.ToArray());
            }
        }
    }
}
Posted
Updated 19-Dec-15 2:46am
v4
Comments
phil.o 19-Dec-15 9:51am    
Help with which problem? You did not state any.
Suvendu Shekhar Giri 19-Dec-15 10:28am    
If you want us to help you, please describe your problem clearly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900