HighTechTalks DotNet Forums  

<% =expression%> not evaluated in custom control attributes

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss <% =expression%> not evaluated in custom control attributes in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
James Hawkins
 
Posts: n/a

Default <% =expression%> not evaluated in custom control attributes - 12-13-2006 , 10:38 AM






Let's say my ASPX page codebehind has a method S() which returns a
string value (of let's say a constant "string").

If I do this (hope all these special chars make it through) on an ASPX
page:
<a href='<%=S()%>'>Link</a>

this renders as one might expect; i.e. S() is evaluated and the html
rendered will be something like:
<a href='string'>Link</a>

With a server control:
<asp:HyperLink
runat="server"
NavigateUrl='<%=S()%>'>
Link
</asp:HyperLink>

I instead get:
<a href='<%=S()%>'>Link</a>

i.e. the expression is not evaluated. This appears to be the same for
most or perhaps all attributes of most or perhaps all server controls.

I'm creating a custom control (derived from WebControl) where I would
prefer to have these expressions evaluated in attributes (or at least
some of them) before they are passed to the control. I'd imagine that
this is probably done by specifying an attribute for the control
property or perhaps for the control as a whole, as the evaluation
needs to occur prior to the attribute value being passed to the
control, which has no context for evaluation at that point.

The control in question is not data bound.

Is this possible? Is it perhaps possible with a different syntactical
construction; e.g. perhaps with <%# %>?

(VS2005, ASP.NET2.0)

Reply With Quote
  #2  
Old   
James Hawkins
 
Posts: n/a

Default Re: <% =expression%> not evaluated in custom control attributes - 12-13-2006 , 10:55 AM






On Wed, 13 Dec 2006 15:38:39 GMT, James Hawkins <not (AT) real (DOT) address>
wrote:

Quote:
The control in question is not data bound.

Is this possible? Is it perhaps possible with a different syntactical
construction; e.g. perhaps with <%# %>?
I noticed that I can use <%#S()%>, but only if I call DataBind() on
the control. As this is a custom control that I don't want to have to
explicitly DataBind(), I am currently doing a 'self-bind' by calling
DataBind() in the control's OnInit(). Is this an acceptable way of
doing things?

Thanks.


Reply With Quote
  #3  
Old   
Axel Dahmen
 
Posts: n/a

Default Re: <% =expression%> not evaluated in custom control attributes - 12-15-2006 , 10:22 PM



Hi James,

instead of using this (obsolete) ASP expression syntax you should assign the NavigateUrl property in your code-behind file:

void Page_Load(...)
{
myHyperLink.NavigateUrl = S();
}

HTH,
Axel Dahmen
www.dashop.de


----------------------
"James Hawkins" <not (AT) real (DOT) address> schrieb im Newsbeitrag news:et50o2d5v667ds9t58ap1f8ouqfec4n8cp (AT) 4ax (DOT) com...
Quote:
Let's say my ASPX page codebehind has a method S() which returns a
string value (of let's say a constant "string").

If I do this (hope all these special chars make it through) on an ASPX
page:
a href='<%=S()%>'>Link</a

this renders as one might expect; i.e. S() is evaluated and the html
rendered will be something like:
a href='string'>Link</a

With a server control:
asp:HyperLink
runat="server"
NavigateUrl='<%=S()%>'
Link
/asp:HyperLink

I instead get:
a href='<%=S()%>'>Link</a

i.e. the expression is not evaluated. This appears to be the same for
most or perhaps all attributes of most or perhaps all server controls.

I'm creating a custom control (derived from WebControl) where I would
prefer to have these expressions evaluated in attributes (or at least
some of them) before they are passed to the control. I'd imagine that
this is probably done by specifying an attribute for the control
property or perhaps for the control as a whole, as the evaluation
needs to occur prior to the attribute value being passed to the
control, which has no context for evaluation at that point.

The control in question is not data bound.

Is this possible? Is it perhaps possible with a different syntactical
construction; e.g. perhaps with <%# %>?

(VS2005, ASP.NET2.0)


Reply With Quote
  #4  
Old   
Rick Strahl [MVP]
 
Posts: n/a

Default Re: <% =expression%> not evaluated in custom control attributes - 12-25-2006 , 07:44 PM



<%# %> expressions require databinding and can only be used inside of
templates or inside of a full attribute value of a control. But the
databinding is required as these expressions generate databinding event
code.

<%= %> doesn't work inside of server control attributes as you found out
because the server control parsing will interpret the expression as a
string. Outside of quotes the the quotes but inside of the server expression
they are not allowed at all.

As Axel points out the better way to do this is to use codebehind to set the
value and while I don't agree about the 'obsolete' comment he makes, in this
case that may be the only way that you can get this to work...

+++ Rick ---


"James Hawkins" <not (AT) real (DOT) address> wrote

Quote:
On Wed, 13 Dec 2006 15:38:39 GMT, James Hawkins <not (AT) real (DOT) address
wrote:

The control in question is not data bound.

Is this possible? Is it perhaps possible with a different syntactical
construction; e.g. perhaps with <%# %>?

I noticed that I can use <%#S()%>, but only if I call DataBind() on
the control. As this is a custom control that I don't want to have to
explicitly DataBind(), I am currently doing a 'self-bind' by calling
DataBind() in the control's OnInit(). Is this an acceptable way of
doing things?

Thanks.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.