954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to create a dynamic dropdownlist?

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..

ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 
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..

ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

sethu84
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

See following to create dynamic dropdown list with event handler
http://urenjoy.blogspot.com/2009/03/create-dynamic-dropdownlists-in-aspnet.html

danidev
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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();

      
        
    }
greeny_1984
Posting Whiz
372 posts since Apr 2007
Reputation Points: 25
Solved Threads: 29
 

To view Complete solution :

Thanks.

antonyvijayanv
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

mbaocha
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You