Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ASP.NET <%= tags in server control attributes?

Tags:

asp.net

tags

This works:

<span value="<%= this.Text %>" /> 

This doesn't work:

<asp:Label Text="<%= this.Text %>" runat="server" /> 

Why is that?

How can I make the second case work properly, i.e., set the label's text to the value of the "Text" variable?

like image 693
Joao Silva Avatar asked Sep 08 '09 11:09

Joao Silva


People also ask

Does adding a runat server attribute to an HTML element change anything?

HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control.

What is runat server attribute?

The runat attribute basically tells ASP.Net that it needs to parse the element, its attributes and it's contents as a server control. Enabling code, on the server, to be executed to configure the response. Without it, any child controls contained within the <head> section will not get parsed.

Why do we add runat server to a web control?

Runat='Server ' Indicates the accessibility of the control at Serverside. Let Me make you more clear about it. If you puts runat="server" inside any of the control then you can use that control at the server side.

What are web controls in ASP.NET what is runat server?

The HTML server controls are HTML elements that include a runat=server attribute. The HTML server controls have the same HTML output and the same properties as their corresponding HTML tags. In addition, HTML server controls provide automatic state management and server-side events.


2 Answers

Use Data binding expressions

<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label> 

Code behind,

protected void Page_Load(object sender, EventArgs e){   DataBind(); } 
like image 108
KV Prajapati Avatar answered Sep 23 '22 02:09

KV Prajapati


you can do this

 <asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label> 
like image 43
user1855575 Avatar answered Sep 24 '22 02:09

user1855575