简体   繁体   中英

How can I add data to my dropdown list box in ASP.NET VB.NET?

How can I add a data to my drop down list came from database? My code is this. And when I try using this:

<form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [renovationproject]"></asp:SqlDataSource>
    <br />

Using Dropdownlist input web control:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"          DataTextField="tasks" DataValueField="workid">
</asp:DropDownList>

It got errors in a webpage that says controls are not allowed to be put here. I only have an empty dropdown right now.

<asp:DropDownList ID="DdLocation" runat="server"></asp:DropDownList>

There are many ways to add data. You can put them in there literally <asp:listitem> ; Or here are some basic ones: http://www.w3schools.com/aspnet/aspnet_databinding.asp

You can also look into creating a <asp:datasource> . I recommend you do this with the wizard first (in desing view, you should have a little arrow block near the dropdown list -- start with "choose data source".

You can also bind with a datatable in your code behind. Google keywords asp:dropdownlist, databind(), dataadapter, dataset, datatable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM