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)?
<custom:Header runat='server' Text="<%# Foo.Bar %>"/>
(note hash instead of equal sign) does not work as well.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
It means to perform a bitwise operation with the values on the left and right-hand side, and then assign the result to the variable on the left, so a bit of a short form.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With