hello,
in my webpage i boud dropdowdlist by sqldatasource.
now i wan to set the selected item 3 of this dropdwonlist
for example:dropdownlist bound with sqldatasource and dropdownllist has a,b,c,d
now i want to search the item in dropdownlist b then select b on page load
how i do that my code not working
i use this code on page load
dropdownlist.SelectedIndex=convert.toint32(dropdownlost.items.findbytext("b").text);
not working
Loading

Mukesh KumarPosted Jan 12, 2012, 7:45 AM
Try this
dropdownlist.SelectedValue = dropdownlist.Items.FindByText("b").Value;
Dharmesh SharmaPosted Jan 12, 2012, 6:58 AM
let me try
thanks
Krishna GaradPosted Jan 12, 2012, 6:51 AM
Best luck
Dharmesh SharmaPosted Jan 12, 2012, 6:30 AM
cos whatever value in dropdown as i save my database in another page
i mean if dropdown has list of country like India,Pakistan then i search like India not india
so its shuld me given me any result apart -1
southing wrong
Krishna GaradPosted Jan 12, 2012, 6:16 AM
string _item="b";
string _item1=_item.ToUpper();//or tolower
and then pass it to ListItem b = new ListItem(_item1);
//this is because of your dropdownlist might contains either uppercase or lowercase and your passing opposite to it. Correct it will work.
Krishna GaradPosted Jan 12, 2012, 6:11 AM
ListItem b = new ListItem("b");//here
here i'm giving "b" if it is present then it will return the index of b if it is not present then it will return -1 only. So just follow the case of "b" i.e. uppercase or lowercase.
Dharmesh SharmaPosted Jan 12, 2012, 6:11 AM
how to access sqldatasource in code bhind
Dharmesh SharmaPosted Jan 12, 2012, 6:06 AM
so i cant do that find another solution
try another
Krishna GaradPosted Jan 12, 2012, 5:39 AM
Page_Load
{
if(!IsPostBack)
{
DataSet ds=GetValuesFromDataBase();//This is you method to get dataset from database.
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="TextFieldWhichYouWnatToShow";
DropDownList1.DataValueField="ValueFieldWhichYouWantToShow";
DropDownList1.DataBind();
}
}
Dharmesh SharmaPosted Jan 12, 2012, 5:34 AM
thanks
Krishna GaradPosted Jan 12, 2012, 5:31 AM
ListItem b = new ListItem("b");
this "b" must be present in your dropdownlist else it will not work. try instead of sqldatasource by manual binding. In this way like
if (!IsPostBack)
{
DropDownList1.Items.Add("a");//here you can bind it manually also
DropDownList1.Items.Add("b");
DropDownList1.Items.Add("c");
DropDownList1.Items.Add("d");
ListItem b = new ListItem("b");
int _index =Convert.ToInt32(DropDownList1.Items.IndexOf(b));
DropDownList1.SelectedIndex = _index;
}
Dharmesh SharmaPosted Jan 12, 2012, 5:27 AM
the problem is i bound dropdownlist by sqldatasource on aspx page thats wy not find the value in dropdownlist on page load
plz help me again
thanks
Krishna GaradPosted Jan 12, 2012, 5:18 AM
ListItem b = new ListItem("b");
int _index =Convert.ToInt32(DropDownList1.Items.IndexOf(b));
DropDownList1.SelectedIndex = _index;