3

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

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

5 回答 5

3

尝试在后面的代码中执行此操作:

       theLinkButton.OnClientClick = 
"return confirm('Are you sure you want to remove Contract Period " +  
    Server.HtmlEncode(ContractPeriod_N.Text) + "?');"; 
于 2009-10-30T18:53:18.513 回答
1

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

<asp:LinkButton ... OnClientClick=
    "<%= "return confirm('Are you sure you want to remove Contract 
        Period " + ContractPeriod_N.Text + "?');" %>" />
于 2009-10-30T18:40:55.760 回答
1

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

于 2009-10-30T18:42:15.290 回答
1

在此处查看我对另一个问题的回答。我相信您可以使用类似于的自定义 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 %>?');" />
于 2009-10-30T18:53:16.907 回答
0

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

<asp:LinkButton runat="server" Text="Hello" OnClientClick='<%# String.Format("return confirm(\"Are you sure you want to remove Contract Period {0}?\");", ContractPeriod_N.Text) %>' />
于 2009-10-30T20:36:24.450 回答