tags:

views:

79

answers:

4

The examples given below could make little sense, but it is because I am focusing on syntax.

Let's say I have such C# code:

public static class Foo
{
  public const string Bar = "hello world.";
}

Now, I would like to use Foo.Bar constant in ASP.Net instead of typing each time "hello world.". So I used this syntax:

<p><%= Foo.Bar %></p>

It works. It works also in such cases:

<p>"<%= Foo.Bar %>"</p>

<p class="<%= Foo.Bar %>">the weird, nonsense example</p>  

So, it works with quotes too. However I have also cases, when quotes get higher priority:

<custom:Header runat='server' Text="<%= Foo.Bar %>"/>

( header is a custom control -- it simply adds some css by default and position ). In such case quotes marks have higher priority and entire text is sucked as-is, in effect I get header with text

<%= Foo.Bar %>

So, my question is -- what is the syntax to get the value of C# constant, no matter what (IOW -- with highest priority)?

Edits:

<custom:Header runat='server' Text="<%# Foo.Bar %>"/>

(note hash instead of equal sign) does not work as well.

A: 

You need to import namespace in your asp.net page <% import namespace="namespace.of.foo.class" %>

Oops, sorry, you can't use <%= syntax in server controls. In case of server controls you need to assign it in code. And it doesn't matter if it is constant or just your page class's property.

Danil
It is really limited to <%= ? Because <%# works for server controls, so it is strange. After all, I could write simply function GetThis, and then write <%# GetThis(Foo.Bar) %>
macias
Hmm, It seems that <%# Foo.Bar %> should work too.
Danil
Unfortunately, it does not work.
macias
+1  A: 

Try to avoid having c# code other than in the code behind. Better put a label control in the aspx page and set it's text property with Foo.Bar

myLabel.Text = Foo.Bar;

You then have all server side code in the code behind, it is much cleaner and readable for others.

elsni
Thank you, it works nicely, but I rather prefer putting symbols in ASP because the code is much shorter, and there are a lot of cases where I don't have to set Id for widgets. With your approach I would have to do this. Anyway, thank you for sharing this -- better to make a choice knowing what options are.
macias
thats agile :-) Thank you too for respecting other people's opinion!
elsni
A: 

afaik the value in <%= is used during the render stage of the page lifecycle

the controls need those values earlier in the lifecycle. Using <%# will happen during databinding.

Another option is to just set it on Page_Load. Its the way its supposed to be used in regular asp.net. Alternatively you can set it even earlier / during init (do if you are not manipulating it).

eglasius
I use Page_Load now thanks to Elsni, but I would like to know how to do it in ASP, <%# also has priority not sufficient enough to resolve the value of it.
macias
do you get the same behavior? you should be getting empty if you don't actually call DataBind on that control.
eglasius
I get the text, it is not empty.
macias
A: 

You can use databinding expressions in your page as long as the page is databound. You can still use your example:

<custom:Header runat='server' Text="<%# Foo.Bar %>"/>

But you'll also need to ensure that you call DataBind() in your code behind to databind all expressions in your page that are outside of a databinding control.

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}
Wallace Breza
Thank you, I added this, but the text property still gets the verbatim quote instead of "hello world".
macias
You get an additional quote? All this should do is bind the value of your constant to the text property of your custom control. I don't have the code for your custom control, but when I try this with a simple `<asp:Label />` control it works fine.
Wallace Breza
No, I get exactly what I typed, so the output is: <%# Foo.Bar %>. Or IOW the text given is treated, well, like a normal text.
macias