内联数据绑定 asp.net 标签未执行

发布于 2024-08-12 05:00:37 字数 513 浏览 1 评论 0原文

我的问题是我以前能够做到这一点,

< div runat="服务器"visible='<%#CallAFunctionThatReturnsBoolean() %>' >

当隐式调用控件的 DataBind 函数并且正确设置 div 的可见性时,将在 Page_Load 中调用 CallAFunctionThatReturnsBoolean()。

现在,由于某种原因,这种情况不再发生,为了使其工作,我必须在我的基本 Page 类中调用 Page.DataBind() 或在该页面的 Page_Load 子类中调用 Me.DataBind() ,但我不这样做我真的不想这样做,特别是在基本 Page 类中,因为如果我有一个页面,假设其中有一个 DataGrid,并且我已经显式调用 DataBind() 函数,那么这个 DataGrid 将被绑定两次,一次来自 Page。 DateBind 和一次来自显式调用 datagrid.DataBind()。

知道为什么不再隐式调用控件的数据绑定事件吗?

谢谢

My problem is I used to be able to do this,

< div runat="server" visible='<%#CallAFunctionThatReturnsBoolean() %>' >

and CallAFunctionThatReturnsBoolean() will be called in Page_Load when the control's DataBind function gets called implicitly and the div's visibility will be set correctly.

Now for some reason this doesn't happen anymore, and to make it work I would have to either call Page.DataBind() in my base Page class or Me.DataBind() in the Page_Load sub in that page, but I don't really want to do this, especially in the base Page class because then if I have a page with let's say a DataGrid in it that I already call the DataBind() function explicitly, then this DataGrid will get bound twice, once from Page.DateBind and once from the explicit call datagrid.DataBind().

Any idea why the control's data binding event is not called implicitly anymore?

Thanks

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

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

发布评论

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

评论(1

┾廆蒐ゝ 2024-08-19 05:00:37

<%# 发生在数据绑定时,<%= 将始终在构建页面时发生,无论任何数据绑定如何。听起来这就是您要找的东西?

此外,数据绑定是控件级别的,因此如果您“数据绑定”网格,它不会对任何其他控件进行数据绑定。即使嵌入的模板化控件在调用网格时也不会自动进行数据绑定,除非您进行连接来执行此操作。

尝试执行以下操作,看看它是否可以解决您的问题:

<div runat="server" visible='<%= CallAFunctionThatReturnsBoolean() ? "true" : "false" %>' >

如果您需要它在数据绑定事件中发生,我更喜欢实现 OnDataBinding 服务器端,如下所示:

// in your aspx
<div runat="server" OnDataBinding="yourDiv_DataBinding">

// in your .cs
protected void yourDiv_DataBinding(object sender, EventArgs e)
{
    HtmlControl div = (HtmlControl)(sender);
    div.Visible = CallAFunctionThatReturnsBoolean();
}

The <%# happens for databinding, the <%= will happen always when the page is being built reglardless of any databinding. It sounds like that is what you are looking for?

Also databinding is control level so if you 'DataBind' a grid, it will not databind any other controls. Even embedded templated controls will not be automatically databound when the grid is called unless you wire the up to do so.

Try doing the following and see if it corrects your problem:

<div runat="server" visible='<%= CallAFunctionThatReturnsBoolean() ? "true" : "false" %>' >

If you require it to occur in the databinding event, I prefer to implement OnDataBinding server side as follows:

// in your aspx
<div runat="server" OnDataBinding="yourDiv_DataBinding">

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