ascx 中的内联动态字符串

发布于 2024-08-09 16:42:27 字数 273 浏览 3 评论 0原文

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

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

This doesn't execute the delimiter (its displayed verbatim in the confirm dialog). Why not? Also, that variable is set in the codebehind, but is ready by the time PreRender gets called, so I should be OK right?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

节枝 2024-08-16 16:42:27

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

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

Try doing it in the code behind:

       theLinkButton.OnClientClick = 
"return confirm('Are you sure you want to remove Contract Period " +  
    Server.HtmlEncode(ContractPeriod_N.Text) + "?');"; 
权谋诡计 2024-08-16 16:42:27

您需要设置该属性,以便它全部来自渲染块或完全不来自渲染块。尝试一下

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

You need to set the property so that it is all from a render block or completely with out. Give this a try

<asp:LinkButton ... OnClientClick=
    "<%= "return confirm('Are you sure you want to remove Contract 
        Period " + ContractPeriod_N.Text + "?');" %>" />
oО清风挽发oО 2024-08-16 16:42:27

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

Of course it's not executed. It's in the middle of a string literal. What would do if you wanted to have the <% text in a string somewhere?

拥醉 2024-08-16 16:42:27

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

/// <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);
    }
}

您的标记的自定义 ExpressionBuilder 来完成您想要的任务,然后您的标记将如下所示:

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

See my answer to a different question here. I believe you can accomplish what you want using a custom ExpressionBuilder similar to

/// <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);
    }
}

Your markup would then look like:

<asp:LinkButton ... OnClientClick=
"return confirm('Are you sure you want to remove Contract 
    Period <%$ Code: ContractPeriod_N.Text %>?');" />
末骤雨初歇 2024-08-16 16:42:27

如果您使用数据绑定,则可以这样设置

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

If you are using databinding then you can set it this way

<asp:LinkButton runat="server" Text="Hello" OnClientClick='<%# String.Format("return confirm(\"Are you sure you want to remove Contract Period {0}?\");", ContractPeriod_N.Text) %>' />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文