gpt4 book ai didi

asp.net - 更改嵌套转发器内下拉列表的选定值

转载 作者:行者123 更新时间:2023-12-02 13:46:31 25 4
gpt4 key购买 nike

我尝试从 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.

最佳答案

设置 DropdownListSelectedItem 的最安全方法是设置 SelectedIndex,如下所示:

ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString()));

关于asp.net - 更改嵌套转发器内下拉列表的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21968178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com