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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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)
    James Hawkins Guest

  2. Similar Questions and Discussions

    1. Designer/attributes for custom DataSource Control?
      Hi, I've developed my own datasource control. When I make my datasource control be the datasource of a formview control, I would like the...
    2. Custom Control does not allow access to Attributes
      I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity...
    3. XHTML and custom attributes
      I'm planning my strategy to be XHTML ready for "Whidbey". My ASP.NET controls have a lot of client-side scripting. Sometimes I pass along custom...
    4. Custom TextBox with custom attributes and properties question
      I have an webform that has a datarepeater on it. In that repeater I'm binding some data and I have put a textbox control in there. On the...
    5. ControlDesigner not invoked on custom control when control is rendered within another custom control
      I have a custom control that has a simple designer (derived from System.Web.UI.Design.ControlDesigner) associated with it (using the...
  3. #2

    Default Re: <% =expression%> not evaluated in custom control attributes

    On Wed, 13 Dec 2006 15:38:39 GMT, James Hawkins <not@real.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.
    James Hawkins Guest

  4. #3

    Default Re: <% =expression%> not evaluated in custom control attributes

    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
    [url]www.dashop.de[/url]


    ----------------------
    "James Hawkins" <not@real.address> schrieb im Newsbeitrag news:et50o2d5v667ds9t58ap1f8ouqfec4n8cp@4ax.com...
    > 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)
    Axel Dahmen Guest

  5. #4

    Default Re: <% =expression%> not evaluated in custom control attributes

    <%# %> 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@real.address> wrote in message
    news:6i80o2935errerrbcnf98d9d96fit77rjo@4ax.com...
    > On Wed, 13 Dec 2006 15:38:39 GMT, James Hawkins <not@real.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.
    Rick Strahl [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139