ascx中的内联动态字符串

时间:2009-10-30 18:38:32

标签: asp.net ascx delimiter

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

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

5 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

当然没有执行。它位于字符串文字的中间。如果你想在某个字符串中包含<%文本会怎么做?

答案 3 :(得分:1)

请参阅我对其他问题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 %>?');" />

答案 4 :(得分: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) %>' />