hi all,
do anyone knows how to create a dynamic dropdownlist? the dropdownlist will retrieve the list items from database..

eg: below is code for static dropdownlist where i declare the listitem myself.. but how should i declare listitem retreive from database?

<asp:dropdownlist id="ddl" Runat="server">		
             <asp:ListItem Value="1">A</asp:ListItem>	<asp:ListItem Value="2">B</asp:ListItem>	<asp:ListItem Value="3">C</asp:ListItem>
</asp:dropdownlist>

thanks..

Recommended Answers

All 8 Replies

what kind of database are you using, SQL, access?

if you can get the information out of the database and into a DataTable all you need to do is:

DropDownList1.DataSource=dataTableName;
			DropDownList1.DataTextField="NameofDatabaseColumn";
			DropDownList1.DataValueField="NameofDatabaseColumn";
			DropDownList1.DataBind();

if you need help getting it from the database into the datatable let me know

what kind of database are you using, SQL, access?

im using ms sql server 2000.. well, i will try the code later

thanks 4 ur help..

how to create an dynamic event handler for dropdownlist in c#

hi,

check this code

<asp:DropDownList ID="ddlist" runat="server" ></asp:DropDownList>
  protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
         
            binddropdown();
           
        }
         }


    public void binddropdown()
    {
        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        ddlist.AppendDataBoundItems = true;
        ListItem li = new ListItem();
        li.Text = "Select";
        li.Value = "Select";
        ddlist.Items.Add(li);
        ddlist.DataSource = ds.Tables[0].DefaultView;
        ddlist.DataTextField = "CategoryName";
        ddlist.DataValueField = "CategoryID";
        ddlist.DataBind();

      
        
    }

To view Complete solution :

<URL SNIPPED, BROKEN LINK>

Thanks.

Which version of dot net are you using? I think creating a dynamic list box should be much of a difficult thing....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.