我尝试从 C# 代码更改下拉列表选定的值:
<repeater >
<repeater>
<dropdown>
我将 aspx 页面中的数据源添加到下拉列表中
一切正常..但我想更改下拉列表中选定的值
编辑表格时能够向用户显示之前选择的名称
protected void PrepairDropDownList(object sender,EventArgs e)
{
shiftrepeater.DataBind();
if (shiftrepeater.Items.Count > 0)
{
for (int shiftcount = 0; shiftcount < shiftrepeater.Items.Count; shiftcount++)
{
Repeater temp = (Repeater)shiftrepeater.Items[shiftcount].FindControl("saturdayrepeater");
if (temp.Items.Count > 0)
{
for (int count = 0; count < temp.Items.Count; count++)
{
DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");//contain the id if the field
SarcShiftUser user = CRUD<SarcShiftUser>.Get(int.Parse(hf.Value)); //a method to select a user with a specific id and add it to object from class sarcshiftuser
if (user.id == 0)
ds.SelectedValue = "";
else
{
ds.SelectedValue = user.user_id + "";
}
}
}
}
}
shiftrepeater.DataBind();
}
我将此方法添加到中继器的 Onload 中:
但没有任何变化,以前的名字也没有显示
P.S:用户对象是正确的,user.user_id也是正确的
P.S 2:我尝试添加 ds.DataBind();更改所选内容后但出现错误:
System.ArgumentOutOfRangeException: 'userdropdown' has a SelectedValue which is invalid because it does not exist in the list of items.
最安全的设置方式SelectedItem
of a DropdownList
是设置SelectedIndex
像这样:
ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString()));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)