DropDownList.SelectedValue 更改(作为 FormView 中的子控件)不坚持
好的,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到了。 我仍然不明白为什么它不能仅在 FormLoad 上工作,但在 FormView 的 DataBound 事件中执行更改可以解决问题。
因此,我想一般的经验法则是,如果在使用数据绑定时对控件进行更改时遇到问题,请尝试在绑定后立即进行更改。
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.
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.
我在下拉列表中遇到问题,并使第一个值显示为“请选择一个值...”,但没有使其成为实际的可选项目,也没有显示在下拉列表中。 我在 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.