Combining Strings in ASP.NET Attributes

后端 未结 3 1332
情深已故
情深已故 2021-01-13 14:22

I am trying to concat a string inside the attribute. I get an error. I think that it is related to my Eval. Is there a proper way to concat strings, or is this

3条回答
  •  礼貌的吻别
    2021-01-13 15:01

    Short answer: NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'

    Longer explanation:

    The problem in your code is that you are use data binding expression only for part of your web control attribute. You need to move everything inside the data binding expression.

    First of all, a data binding expression is this:

    <%# EXPRESSION %>
    

    Basically the rule for using a data binding expression for a web control attribute is that the expression must be the only thing in the attribute:

    
    

    So your first attribute, Text, is correct. But your second attribute, NavigateUrl is not correct. Because you put ViewItem.aspx?id= as the value for the attribute, leaving + '<%# Eval("Key.Id") %>' outside any attribute but inside the control tag.

    Here is the correct syntax:

    
    

    Notice that we are using a format string as the second parameter for Eval(). This is equivalent to the following, more explicit, syntax:

    
    

提交回复
热议问题