views:

300

answers:

5

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 %>?');" />
A: 

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 + "?');" %>" />
Bob
You can't use <%= %> inside server controls- you will get Server tags cannot contain <% ... %> constructs compiler error if you try that!
RichardOD
+1  A: 

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?

Joel Coehoorn
i kinda chalked it up to magic =o there seems to be a lotta that in asp
Dustin Getz
+1  A: 

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 %>?');" />
Chris Shouts
+2  A: 

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) + "?');";
RichardOD
A: 

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) %>' />
Mike J