Reputation: 39
How to display a list of items in a ListBox
while choose a particular item in dropdown list?
Example: Thiru is a developer he done some modules. In dropdown list has a list of developers. While choose Thiru in this list I want to display what are all the modules completed by Thiru that can be listed in listbox.
I am using Visual Studio 2008, C# with ASP.NET
Upvotes: -1
Views: 9411
Reputation: 29657
On the listbox databind your items to a datasource and add a parameter with the value from you drop down list.
This is roughly what you need to do.
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="listDataSource"
DataTextField="Field" DataValueField="Field"></asp:ListBox>
<asp:SqlDataSource ID="listDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:XXXX %>"
SelectCommand=
"SELECT [Field] FROM [LisotOfModules] WHERE ([DevID] = @DevID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DevID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="ddlDatasource" DataTextField="DevID"
DataValueField="DevID">
</asp:DropDownList>
<asp:SqlDataSource ID="ddlDatasource" runat="server"
ConnectionString="<%$ ConnectionStrings:XXX %>"
SelectCommand="SELECT [DevID] FROM [Developers]">
</asp:SqlDataSource>
</form>
Upvotes: 0
Reputation: 9781
If your data source is just a List or similar, you can simply do the following:
myListBox.DataSource = myDataSource;
myListBox.DataBind();
If your datasource is a class or list of classes, you need to specify which property to display and which to set as the value.
As above:
myListBox.DataSource = myDataSource;
myListBox.DataTextField = "MyPropertyNameOnMyClass"; //This will be displayed
myListBox.DataValueField = "MyValuePropertyOnMyClass";
myListBox.DataBind();
Upvotes: 0