Tuesday, March 27, 2012

binding a database records to a dropdownlist

Hey I just wanted to know if anyone could tell me how to bind values from a database to a dropdownlist?

DropDownlist.DataSource = dataset; // dataset returned from the database

DropDownlist.DataBind();

|||

Hi here is a detailed code fromdata bind a DropDownList declaratively to a data source control:

<html><head> <script language="VB" runat="server"> Sub SubmitBtn_Click(sender As Object, e As EventArgs) Label1.Text = "You chose: " + DropDown1.SelectedValue End Sub </script></head><body> <h3><font face="Verdana">DataBinding DropDownList</font></h3> <form runat=server> <asp:DropDownList id="DropDown1" DataSourceID="SqlDataSource1" DataTextField="au_lname" DataValueField="au_id" runat="server" /> <asp:SqlDataSource id="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:Pubs%>' SelectCommand="select DISTINCT au_id, au_lname from authors" runat="server"/> <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat=server/> <p> <asp:Label id=Label1 Font-Names="Verdana" font-size="10pt" runat="server" /> </form></body></html>
Hope it helps.

No comments:

Post a Comment