简体   繁体   English

如何在服务器控件属性中使用ASP.NET <%=标记?

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

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, ie, set the label's text to the value of the "Text" variable? 如何使第二种情况正确工作,即将标签的文本设置为“ Text”变量的值?

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();
}

你可以这样做

 <asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>

In my code i am using something like this easily but in the databound control like ListView Item template 在我的代码中,我很容易使用这种方法,但是在数据绑定控件(如ListView Item模板)中

 <asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />

But when i tried to use outside the databound control using <%# .. %>, it simply doesn't work. 但是,当我尝试使用<%#..%>在数据绑定控件之外使用时,它根本不起作用。

You can easily do with 您可以轻松地做到

<a href="<%=myHref%>">My href</a> 

But for server controls, and outside of databound control. 但是对于服务器控件,以及数据绑定控件之外。 We need to call DataBind() in pageload event explicitly 我们需要在pageload事件中显式调用DataBind()

<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >

You will need to set the value of the server control in code 您将需要在代码中设置服务器控件的值

First of all, assign an ID to the label control so you can access the control 首先,为标签控件分配一个ID,以便您可以访问该控件

<asp:Label ID="myLabel" runat="server" />

Then, in your Page_Load function, set the value of your labels 'Text' field 然后,在您的Page_Load函数中,设置标签“文本”字段的值

protected void Page_Load(object sender, EventArgs e)
{
    myLabel.Text = 'Whatever you want the label to display';
}

This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need 此功能将在文件背后的代码中,或者,如果您不使用模型背后的代码,则将在aspx页面中

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        myLabel.Text = 'Whatever you want the label to display';
    }
</script>

Good luck. 祝好运。

<asp:Label> is compiling at runtime and converting to html tags. <asp:Label>正在运行时进行编译并转换为html标签。 You can set text with codebehind or like this: 您可以使用codebehind或以下方式设置文本:

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

UPD: Seems like my variant doesnt work, this is better: UPD:似乎我的变体不起作用,这更好:

protected void Page_Load(object sender,EventArgs e) 
{
    Text1.Text = this.Text;
}

Not sure how to mark this as such, but this is a bit of a duplicate. 不知道如何这样标记,但这有点重复。 See this thread . 看到这个线程

I don't think embedding code in to your markup will really make your markup any clearer or more elegant. 我认为将代码嵌入标记中不会真正使标记更清晰或更优雅。

Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/ 只是为那些希望对该问题进行良好技术分解的人提供这个小块-https: //blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp -净/

I think the crux is in pretty decent agreement with the other answers: 我认为问题的关键在于其他答案:

  • The <%= expressions are evaluated at render time <%=表达式在渲染时求值
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called. <%#表达式在DataBind()时计算,如果不调用DataBind(),则根本不计算。
  • <%# expressions can be used as properties in server-side controls. <%#表达式可用作服务器端控件中的属性。 <%= expressions cannot. <%=表达式不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM