How do I make my ASP.NET server control take an embedded code block as a property value?
Asked Answered
J

5

7

I have a custom server control with a property of Title. When using the control, I'd like to set the value of the title in the aspx page like so:

<cc1:customControl runat="server" Title='<%= PagePropertyValue%>' >
more content
</cc1:customControl>

When I do this, however, I am getting the exact String <%= PagePropertyValue%> being displayed rather than the Property Value that I would like to see.

So after trying the databinding expression (as suggested below). I don't get the string literal that looked bad but I don't get anything else either.

<cc1:customControl runat="server" Title='<%# PagePropertyValue%>' >
more content
</cc1:customControl>

What do I need to do to my custom control to take this sort of value? Or is there something I need to do to the page.

Jauregui answered 22/12, 2008 at 19:56 Comment(0)
S
7

You cant. <%= %> will write the string directly to the response-stream, which happens after the server control is constructed. See this post for an explanation.

So its either codebehind, or <%# + databinding as Zachary suggests.

Senile answered 22/12, 2008 at 20:9 Comment(0)
J
2

As a followup to my own question, I have discovered that what I really wanted was to use ASP.NET Expressions using the <%$ syntax, since what I wanted to do was put in localized content.

This can be done with apparently no extra handling on the server control side.

<cc1:customControl runat="server" Title='<%$ Resouces: ResourceFile, ContentKey %>' >
more content and controls
</cc1:customControl>

This works just fine.

Jauregui answered 29/12, 2008 at 21:13 Comment(0)
F
1

Try using databinding syntax: <%# PagePropertyValue %>

Filmore answered 22/12, 2008 at 20:0 Comment(2)
That made it display blank rather than the actual code string. I am not implementing a databinding event on my control. I get similar results with an asp label control with both settings. (using text property).Jauregui
Try call label.DataBind() and it should display whatever the expression returns.Senile
G
1

For the bind property value to work correctly as suggested, you will have this in the aspx or ascx file :

<cc1:customControl runat="server" Title='<%# PagePropertyValue %>' >
more content
</cc1:customControl>

You will then need to actually bind data in your page wich you have to add this in you code behind file (code in C#)

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

That way it will bind the data in your ascx or aspx file.

Gilmore answered 22/12, 2008 at 20:56 Comment(0)
C
0

Note that this is specific to control attributes. When using the <%= syntax outside control attributes meaning anywhere else in the page the syntax works as expected. So this <%=GetCapitalUserName()%> would call the correct method and inject the result of the call in the page.

Controversial answered 19/5, 2009 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.