views:

608

answers:

2

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

+2  A: 

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();
}
Kelsey
I guess I can make CallAFunctionThatReturnsBoolean to return a string, either "True" or "False", but that won't work. Here is the error message I got, Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%=CallAFunctionThatReturnsBoolean()%>' for the 'Visible' property.Seems to me it takes it literally instead of calling the function.Right, '#' relates to databinding, the thing is it was binding the control before, so it worked fine, but it doesn't work anymore.
epurwaka
No you didn't copy my answer exactly... notice the ? "true" : "false" part???? That takes the value your function returns and maps it to the proper text.
Kelsey
If you go the DataBinding method in my second example then the Visible tag maps directly to a bool so you don't need the conversion to a string snippet.
Kelsey
Kelsey, thanks for your help. <%= won't work with server control, I still get that same error even with the same exact code from what you gave me. I figured it out why it was working before and it's not working now. I used to have Page.DataBind() in Page_Load sub in my base page class and it's removed somehow. Since Page.DataBind() pretty much calls the server control's DataBind, <%# works. I think it's not the ideal solution, I would prefer to set the control's property in the code behind than inline, but I can live with it for now. Thanks
epurwaka
A: 

May be this would be helpful: http://codeshode.blogspot.com/2010/06/inline-aspnet-tags.html

tjaank