Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 551
Jeff Martin Avatar asked Dec 22 '08 19:12

Jeff Martin


2 Answers

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.

like image 59
Tom Jelen Avatar answered Oct 08 '22 01:10

Tom Jelen


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.

like image 33
Jeff Martin Avatar answered Oct 08 '22 00:10

Jeff Martin