Inline dynamic string in ascx

This does not execute the delimiter (its displayed verbatim in the confirmation dialog). Why not? In addition, this variable is set in the code, but is ready by the time PreRender is called, so should I be fine?

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

Try to do this in the code behind:

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

You need to set the property so that it is completely out of the rendering unit or completely disabled. Try

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

, . . , , <% -?

+1

See my answer to another question here . I believe that 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 will look like this:

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

If you use data binding, 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) %>' />
0
source

Source: https://habr.com/ru/post/1721531/


All Articles