比如我的一个dropdownlist里面有很多的记录,我并不知道每条记录的索引号,我只知道里面有一条"李明"的记录,现在我想增加一个
button,点击button,让"李明"选中,该怎么办?很急,在线等待
this.DropDownList1.Items.FindByText("李明").Selected;
ok
笨办法
for(int i=0;i<Dropdownlist1.items.count;i++)
{
if(dropdownlist1.items[i].text="李明")
{
dropdownlist1.selectindex:=i;
break;
}
}
DropDownList1.SelectedIndex = DropDownList1.Items.FindByText("李明").ItemIndex;
page_load:
if (!IsPostBack)
{
DropDownList1.DataBind();
}
this.DropDownList1.Items.FindByText().Selected =true;
<asp:ListItem Value="0" Selected="True">aaa</asp:ListItem>
去掉這Selected="True"
<asp:ListItem Value="0" >aaa</asp:ListItem>
<asp:ListItem Value="0" Selected="True">aaa</asp:ListItem>
去掉這Selected="True"
<asp:ListItem Value="0" >aaa</asp:ListItem>
如果這也不行的話
請在page_load:加上這段代碼:
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
如果還是不行的話,沒辦法了
但是我經常是這樣寫的
ddlst_sex.Items[ddlst_sex.Items.IndexOf(ddlst_sex.Items.FindByText(“李明”))].Selected=true;
Sorry:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf( DropDownList1.Items.FindByText("李明") );
把你全部代码贴出来,改好了还给你
<asp:ListItem Value="0" Selected="True">aaa</asp:ListItem>
去掉這Selected="True"
dp.Items[dp.Items.IndexOf(dp.Items.FindByText(“李明”))].Selected=true;
改成這樣:
this.DropDownList1.SelectedIndex = this.DropDownList1.Items.IndexOf(this.DropDownList1.Items.FindByText("bbb"));
是可以的
这样就可以搞定了
<1>令当前的被选定的ITEM取消选中 DROPDOWNLIST.SELECTEDITEM.SELECTED=FALSE
<2>然后选中你想要选中的ITEM DROPDOWNLIST.ITEMS.FINDBYTEXT("李明").SELECTED=TRUE
试试吧!