Inline Data-Binding asp.net tags not executing

自作多情 提交于 2019-12-11 18:46:43

问题


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


回答1:


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();
}


来源:https://stackoverflow.com/questions/1744947/inline-data-binding-asp-net-tags-not-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!