【问题标题】:inline dynamic string in ascxascx 中的内联动态字符串
【发布时间】:2009-10-30 18:38:32
【问题描述】:

这不会执行分隔符(它在确认对话框中逐字显示)。为什么不?此外,该变量已在代码隐藏中设置,但在调用 PreRender 时已准备就绪,所以我应该没问题吧?

<asp:LinkButton ... OnClientClick=
    "return confirm('Are you sure you want to remove Contract 
        Period <%= ContractPeriod_N.Text %>?');" />

【问题讨论】:

    标签: asp.net ascx delimiter


    【解决方案1】:

    尝试在后面的代码中这样做:

           theLinkButton.OnClientClick = 
    "return confirm('Are you sure you want to remove Contract Period " +  
        Server.HtmlEncode(ContractPeriod_N.Text) + "?');"; 
    

    【讨论】:

      【解决方案2】:

      您需要设置属性,使其全部来自渲染块或完全没有渲染块。试试这个

      <asp:LinkButton ... OnClientClick=
          "<%= "return confirm('Are you sure you want to remove Contract 
              Period " + ContractPeriod_N.Text + "?');" %>" />
      

      【讨论】:

      • 你不能在服务器控件中使用 - 如果你尝试这样做,你会得到服务器标签不能包含 构造编译器错误!
      【解决方案3】:

      当然不会执行。它位于字符串文字的中间。如果您想将&lt;% 文本放在某个字符串中,该怎么办?

      【讨论】:

      • 我把它归结为魔法 =o 在 asp 中似乎有很多
      【解决方案4】:

      查看我对另一个问题的回答 here。我相信您可以使用类似于

      的自定义 ExpressionBuilder 完成您想要的
      /// <summary>
      /// An Expression Builder for inserting raw code elements into ASP.NET markup.
      /// Code obtained from: http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
      /// </summary>
      [ExpressionPrefix("Code")]
      public class CodeExpressionBuilder : ExpressionBuilder
      {
          /// <summary>
          /// Inserts the evaluated code directly into the markup.
          /// </summary>
          /// <param name="entry">Provides information about the expression and where it was applied.</param>
          /// <param name="parsedData">Unused parameter.</param>
          /// <param name="context">Unused paramter.</param>
          /// <returns>A <see cref="CodeExpression"/>.</returns>
          public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
          {
              return new CodeSnippetExpression(entry.Expression);
          }
      }
      

      您的标记将如下所示:

      <asp:LinkButton ... OnClientClick=
      "return confirm('Are you sure you want to remove Contract 
          Period <%$ Code: ContractPeriod_N.Text %>?');" />
      

      【讨论】:

        【解决方案5】:

        如果你使用数据绑定,那么你可以这样设置

        <asp:LinkButton runat="server" Text="Hello" OnClientClick='<%# String.Format("return confirm(\"Are you sure you want to remove Contract Period {0}?\");", ContractPeriod_N.Text) %>' />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-05
          相关资源
          最近更新 更多