DropDownList.SelectedValue 更改(作为 FormView 中的子控件)不坚持

发布于 2024-07-08 23:45:13 字数 485 浏览 9 评论 0原文

好的,我有一个 FormView,在 InsertItemTemplate 中有几个子控件。 其中之一是 DropDownList,称为 DdlAssigned。 我在页面的 OnLoad 方法中引用它,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    ((DropDownList)FrmAdd.FindControl("DdlAssigned")).SelectedValue =
          ((Guid)Membership.GetUser().ProviderUserKey).ToString();
}

基本上,我只是将 DropDownList 的默认值设置为当前登录的用户。

无论如何,当页面完成加载时,SelectedValue 更改不会反映在页面上。 我单步执行 OnLoad,可以看到监视列表中反映的更改,但当一切都说完并完成后,页面上没有任何不同。

Okay, I have a FormView with a couple of child controls in an InsertItemTemplate. One of them is a DropDownList, called DdlAssigned. I reference it in the Page's OnLoad method like so:

protected void Page_Load(object sender, EventArgs e)
{
    ((DropDownList)FrmAdd.FindControl("DdlAssigned")).SelectedValue =
          ((Guid)Membership.GetUser().ProviderUserKey).ToString();
}

Basically I'm just setting the default value of the DropDownList to the user currently logged in.

Anyway, when the page finishes loading the SelectedValue change isn't reflected on the page. I stepped through OnLoad and I can see the change reflected in my Watch list, but when all is said and done nothing's different on the page.

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

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

发布评论

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

评论(2

记忆で 2024-07-15 23:45:13

我想到了。 我仍然不明白为什么它不能仅在 FormLoad 上工作,但在 FormView 的 DataBound 事件中执行更改可以解决问题。

protected void FrmAdd_DataBound(object sender, EventArgs e)
{
    // This is the same code as before, but done in the FormView's DataBound event.
    ((DropDownList)FrmAdd.Row.FindControl("DdlAssigned")).SelectedValue =
        ((Guid)Membership.GetUser().ProviderUserKey).ToString();
}

因此,我想一般的经验法则是,如果在使用数据绑定时对控件进行更改时遇到问题,请尝试在绑定后立即进行更改。

I figured it out. I'm still missing exactly why it doesn't work just on FormLoad, but performing the change in the FormView's DataBound event does the trick.

protected void FrmAdd_DataBound(object sender, EventArgs e)
{
    // This is the same code as before, but done in the FormView's DataBound event.
    ((DropDownList)FrmAdd.Row.FindControl("DdlAssigned")).SelectedValue =
        ((Guid)Membership.GetUser().ProviderUserKey).ToString();
}

So, I guess the general rule of thumb is that if you are having problems making changes to controls when working with databinding, try to make them immediately after it has been bound.

忆梦 2024-07-15 23:45:13

我在下拉列表中遇到问题,并使第一个值显示为“请选择一个值...”,但没有使其成为实际的可选项目,也没有显示在下拉列表中。 我在 page_load 中绑定了 ddl,并且必须确保在与数据绑定后设置下拉列表的文本。 通过将其添加到数据绑定部分,您已经完成了同样的事情。

I had a problem with dropdownlists and making the first value say something like, "Please select a value..." but without making it an actual selectable item, nor show up on the dropdownlist. I was binding the ddl in the page_load and I have to make sure that I set the text of the dropdownlist, AFTER it's been bound with data. You've accomplished the same thing by adding it to your databound section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文