在 DataBind 上选择封装的 DropDownList 中的项目

发布于 2024-08-21 17:11:34 字数 1031 浏览 5 评论 0原文

我有一个复杂的 UserControl ,其主要目的是使用许多属性封装 DropDownList 以进行高级操作。

根据之前设置的属性,在 PreRender 事件上填充列表:

protected void Page_PreRender(object sender, EventArgs e)
{
    sourceClient.SelectCommand = this.Property1 ? "exec a" : "exec b";
}

最常用的属性是 ClientID:

[Category("Settings")]
public int ClientID
{
   get
   {
      return Int32.Parse(DropDownList1.SelectedItem.Value);
   }
   set
   {
      DropDownList1.Items.FindByValue(value).Selected = true;
   }
}

Getter 通常由 SqlDataSources 中的 ControlPameters 调用> 在带有此控件的页面上。

Setter - 来自标记: >

所以问题是:

为什么 Bind 中的 setter 比 PreRender 更早被调用?并且 DropDownList 为空,项目选择不起作用!如何解决此行为?

Edit1:好的,不是 PreRender 而是 Init。但是 DropDownList1_DataBinding 仍在属性设置器之后被调用!

I have a complex UserControl with the main purpose to encapsulate DropDownList with a number of properties for advanced manipulation.

List is being populated on PreRender event depending on properties previously were set:

protected void Page_PreRender(object sender, EventArgs e)
{
    sourceClient.SelectCommand = this.Property1 ? "exec a" : "exec b";
}

The most used property is ClientID:

[Category("Settings")]
public int ClientID
{
   get
   {
      return Int32.Parse(DropDownList1.SelectedItem.Value);
   }
   set
   {
      DropDownList1.Items.FindByValue(value).Selected = true;
   }
}

Getter commonly is being called by ControlPameters in SqlDataSources on pages with this control.

Setter - from markup: <uc:UserControl1 runat="server" ClientID='<%# Bind("ID") %>' />.

So the question is:

Why does setter from Bind is called earlier then PreRender? And DropDownList is empty and item selecting doesn't work! How to workaround this behavior?

Edit1: Ok, not PreRender but Init. But DropDownList1_DataBinding is still being called after property setter!

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

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

发布评论

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

评论(1

污味仙女 2024-08-28 17:11:34

数据绑定始终发生在 PreRender 之前。来自 ASP.Net 页面生命周期

数据绑定
此事件由
之前的数据绑定控件
包含的 PreRender 事件
控件(或 Page 对象的控件)和
标志着绑定的开始
控制数据。

使用此事件手动打开
数据库连接(如果需要)。
(数据源控件通常使
这是不必要的。)

解决您的问题的一种方法是仅处理 DataBinding 事件并预先绑定您的下拉列表(甚至只是在 Load 期间执行),而不是等待一直到PreRender。这将确保 Bind 调用结束时 DropDownList 可用。

另一种解决方案是仅将控件传递给数据源本身的引用,而不是使用 Bind 调用。然后,它可以在正确的时间以编程方式处理自身绑定 - 您可以加载 DropDownList,然后获取它的 ID,这一切都在 PreRender 期间通过访问数据源进行。

DataBinding always occurs before PreRender. From ASP.Net Page Lifecycle:

DataBinding
This event is raised by
data-bound controls before the
PreRender event of the containing
control (or of the Page object) and
marks the beginning of binding the
control to the data.

Use this event to manually open
database connections, if required.
(The data source controls often make
this unnecessary.)

One solution to your issue would be to just handle the DataBinding event and pre-bind your dropdownlist (or even just do it during Load), rather than waiting all the way until PreRender. This would ensure that the DropDownList was available when the Bind call goes off.

Another solution would be to just pass your control a reference to the datasource itself, rather than using a Bind call. Then it could programmatically deal with binding itself at the right times - you could load your DropDownList, and then get your ID for it, all during PreRender, by accessing the datasource.

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