how to hard code values into a dropdownlist and use that selected value in my code behind

Posted by Anishabathija under C# on 1/3/2013 | Points: 10 | Views : 7241 | Status : [Member] | Replies : 3
hey ,

I have numbers 1 to 100.
and i have these values in a txt file. How do I bind it to a dropdownlist. how could i do that.
and how do i refer this selected dropdownlist value in my code behind.
please help.




Responses

Posted by: Saratvaddilli on: 1/3/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Just declare the drop down list in the frond end and in code behind add the values and use selectedindex/value
in aspx:
     <asp:DropDownList ID="DropDownList1" runat="server" Font-Bold="true" Width="180px">

</asp:DropDownList><br />

In code behind:
protected void Page_Load(object sender, EventArgs e)

{
DropDownList1.Items.Clear();
DropDownList3.Items.Add("Sample");
DropDownList3.Items.Add("Demo");
DropDownList3.Items.Add("Example");
}

now we can use
DropDownList1.selectedvalue,

DropDownList1.selectedIndex

to get the selected value to the code behind


Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Posted by: Anishabathija on: 1/3/2013 [Member] Starter | Points: 25

Up
0
Down
hey sarat i have 100 hundred values in another text file, so how do i pass all that using a loop maybe.

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

Posted by: Saratvaddilli on: 1/3/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi Anishabathija ,
you can convert your values as a list and can bind them to the dropdownlist

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Login to post response