Populate Listbox from a Dropdownlist value [Resolved]

Posted by Akiii under C# on 7/5/2011 | Points: 10 | Views : 7642 | Status : [Member] | Replies : 12
Hi all,

Can someone one tell how to populate Listbox by selecting a value from a Dropdownlist ?

Any help is appreciated
Thanks and Regards
Akiii




Responses

Posted by: Ndebata on: 7/7/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Take a look at the attached sample
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 7/11/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Suppose you have a employee table with colums
EmpID, Name,Address,Designation....
Your requirement is....
In the page load you want to show the names in the dropdownlist and when selecting a name all the details of that employee should be listed in the listitem...

Try this....
in aspx..
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl1_SelectedIndexChanged">
</asp:DropDownList>


in the page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Retreive the result set in DataSet ds;
//assume that you know how to get the resultset of a table in the dataset using ado.net
//Query -"SELECT * from employee"
so,
DataSet ds =The ResultSet of your query;
ddl1.DataSource = ds;
ddl1.DataTextField = "Name";
ddl1.DataValueField = "EmpID";
ddlCategory.DataBind();
//So in this way you can show the names in the dropdownlist

}

}

Nitha Deepak

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 7/5/2011 [Member] Starter | Points: 25

Up
0
Down
 ListBox1.Items.Add(DropDownList1.SelectedValue);


Thanks&Regards
LakshmiNarayana Nalluri.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/5/2011 [Member] Bronze | Points: 25

Up
0
Down
thanks Lakn2 for replying.....

any other way to do that ?

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 7/5/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Akiii,

What is the purpose of doing that?
First of all if you just want to show single selected value of dropdownlist then use onselectedindexchanged event
and use DropDownList1.SelectedValue to get the value.

But if you populating the data in listbox based on the value from dropdownlist
Then pass this selected value to get a collection and assign the same to datasource of ListBox and call the databind method of listbox.

Thanks,
Debata

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/6/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Narayan,
Thanks for your reply....

Scenario is this:-
I have a dropdown and depending upon the value selected from dropdown i will fetch the data from the database and populate it in Listbox....

Now how to do that ?

Regards
Akiii


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/7/2011 [Member] Bronze | Points: 25

Up
0
Down
thanks nitha for the help....but i want that the selected value of a dropdown should populate in the listbox..

Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 7/7/2011 [Member] Bronze | Points: 25

Up
0
Down
in the dropdown list you can give text as what you want to display in the ddl and value as the unique id of the corresponding row. Using this id you can retrieve the data from datadase.
Example:-
<asp:DropDownList ID="DropDownList1" runat="server" 

onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True" >
<asp:ListItem Text="-Select Items-"></asp:ListItem>
<asp:ListItem Text="Tomato Soup" Value="TS20"></asp:ListItem>
<asp:ListItem Text="Veg.Corn Soup" Value="VS20"></asp:ListItem>
<asp:ListItem Text="Chicken Soup" Value="CS25"></asp:ListItem>
</asp:DropDownList>


string str = DropDownList1.SelectedItem.Value;
in str we will get the id and using that id we can retrieve data from database.

Nitha Deepak

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi nitha....
Thanks for the code....

All my data are coming from the database including the data in the dropdownlist ...
In this respect what will be my possible doings?
Do let me know your idea....!

Regards
Akiii


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi narayan....

excellent coding style you got....
your sample is of immense help as always....

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/11/2011 [Member] Bronze | Points: 25

Up
0
Down
Thanks nitha for the code...

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response