设置数据绑定 DropDownList 的 SelectedValue

发布于 2024-11-24 14:46:40 字数 309 浏览 1 评论 0原文

我有一个 asp.net dropDownList,它在页面加载时自动绑定到 sqlDataSource 到客户端类型的值。在页面加载时,我还创建了一个 Client 对象,它的属性之一是 ClientType。我试图根据 Client 对象的 ClientType 属性的值设置 ddl 的 SelectedValue ,但未成功。我收到以下错误消息“System.ArgumentOutOfRangeException:'ddlClientType'具有无效的 SelectedValue,因为它不存在于项目列表中”。据我所知,这是因为当我尝试设置所选值时列表尚未填充。有办法克服这个问题吗?谢谢你!

I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values of client type on page load. On page load I am also creating a Client object, one of it's properties is ClientType. I am trying to set the SelectedValue of the ddl according to the value of the ClientType property of the Client object unsuccessfully. I recieve the following error message "System.ArgumentOutOfRangeException: 'ddlClientType' has a SelectedValue which is invalid because it does not exist in the list of items". I understand that this is because the list has not yet been populated when I'm trying to set the selected value. Is there a way of overcoming this problem? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-12-01 14:46:41

在设置选定值之前,检查项目是否在列表中,然后通过索引选择它

<asp:DropDownList id="dropDownList"
                    AutoPostBack="True"
                    OnDataBound="OnListDataBound"
                    runat="server />
protected void OnListDataBound(object sender, EventArgs e) 
{
    int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
    if (itemIndex >= 0)
    {
      dropDownList.SelectedItemIndex = itemIndex;
    }
}

编辑:已添加...

如果您在页面加载中进行绑定操作,请尝试按照以下方式操作:

  • 将所有与绑定相关的代码移至覆盖 DataBind 中() 方法
  • Page 的 Page_Load 中添加:(如果控件不直接调用 DataBind,这是父页面的责任)
if (!IsPostBack)
{
   Page.DataBind(); // only for pages
}

Before setting a selected value check whether item is in list and than select it by index

<asp:DropDownList id="dropDownList"
                    AutoPostBack="True"
                    OnDataBound="OnListDataBound"
                    runat="server />
protected void OnListDataBound(object sender, EventArgs e) 
{
    int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
    if (itemIndex >= 0)
    {
      dropDownList.SelectedItemIndex = itemIndex;
    }
}

EDIT: Added...

If you are doing binding stuff in Page Load, try to follow this way:

  • Move all binding related code in overriden DataBind() method
  • In Page_Load of Page add: (in case of control do not call DataBind directrly, this is a responsibility of a parent page)
if (!IsPostBack)
{
   Page.DataBind(); // only for pages
}
拔了角的鹿 2024-12-01 14:46:40

您必须使用 DataBound 事件,一旦数据绑定完成,它将被触发。

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    // You need to set the Selected value here...
}

如果您确实想查看页面加载事件中的值,请在设置值之前调用 DataBind() 方法。 ..

protected void Page_Load(object sender, EventArgs e)
{
    DropdownList1.DataBind();
    DropdownList1.SelectedValue = "Value";
}

You have to use the DataBound Event, it will be fired, once databinding is complete

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    // You need to set the Selected value here...
}

If you really want to see the value in the Page load event, then call the DataBind() method before setting the value...

protected void Page_Load(object sender, EventArgs e)
{
    DropdownList1.DataBind();
    DropdownList1.SelectedValue = "Value";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文