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

后端 未结 7 1413
陌清茗
陌清茗 2020-11-28 10:15

This works:

\" />

This doesn\'t work:

         


        
相关标签:
7条回答
  • 2020-11-28 10:46

    <asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:

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

    UPD: Seems like my variant doesnt work, this is better:

    protected void Page_Load(object sender,EventArgs e) 
    {
        Text1.Text = this.Text;
    }
    
    0 讨论(0)
  • 2020-11-28 10:48

    In my code i am using something like this easily but in the databound control like ListView Item template

     <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

    <asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >
    
    0 讨论(0)
  • 2020-11-28 10:53

    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.

    0 讨论(0)
  • 2020-11-28 10:53

    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();
    }
    
    0 讨论(0)
  • 2020-11-28 10:55

    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

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

    Then, in your Page_Load function, set the value of your labels 'Text' field

    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

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

    Good luck.

    0 讨论(0)
  • 2020-11-28 10:57

    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/

    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.
    • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.
    0 讨论(0)
提交回复
热议问题