i am using this code to fill dropdownlist from database.
public void fillcountry()
{
BL obj = new BL();
DataSet ds = obj.dss("select * from Country ");
drplistcountry.DataSource = ds;
drplistcountry.DataTextField = "CountryName";
drplistcountry.DataValueField = "CountryId";
drplistcountry.DataBind();
drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
}
i am Using this fillcountry() in page load() event. and Rerutning selecteditm.text on Button Click event
drplistcountry is always showing First index text , How to solve it?
Its is very simple but very difficult to find out, check your value
Datavaluefield
of all the items should not be blank or null.Means:
if value is blank means you cannot will get the first items only.
I was also facing same issue but in my case dropdown value was same for all items, which was causing issue. So, make sure you are binding dropdown list correctly.
:)
In .aspx page:
For Dropdownlist Control set EnableViewState property to true.
In .aspx.cs page:
In PageLoad event check for following:
An easy way You can use repeater to make select in html code like this
for get data from data base use ado.net
and for get selected value. the select element name is ("cat") So
and store to db simply
Try this code
And you shoud call this function only once.
DropdownList selected Item Text value will work like this.