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


Krishna GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 12:18 AM EST
ListItem b = new ListItem("b");
int _index =Convert.ToInt32(DropDownList1.Items.IndexOf(b));
DropDownList1.SelectedIndex = _index;
Dharmesh SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 12:27 AM EST
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 GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 12:31 AM EST
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 SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 12:34 AM EST
thanks
Krishna GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 12:39 AM EST
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 SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 1:06 AM EST
so i cant do that find another solution
try another
Dharmesh SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 1:11 AM EST
how to access sqldatasource in code bhind
Krishna GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 1:11 AM EST
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.
Krishna GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 1:16 AM EST
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.
Dharmesh SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 1:30 AM EST
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 GaradRank #191 · 12.8K reputationPosted Jan 12, 2012, 1:51 AM EST
Best luck
Dharmesh SharmaRank #1,974 · 741 reputationPosted Jan 12, 2012, 1:58 AM EST
let me try
thanks
Mukesh KumarRank #615 · 3.9K reputationPosted Jan 12, 2012, 2:45 AM EST
Try this
dropdownlist.SelectedValue = dropdownlist.Items.FindByText("b").Value;