views:

811

answers:

2

hi

how to display list of items in list box while choose a particular item in dropdown list.

ex: 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 a what are all the modules completed by thiru that can be listed in listbox

I am using Visual Studio 2008,C# with ASP.Net

thanks

thiru
A: 

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>
John Nolan
A: 

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