inline dynamic string in ascx
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 y开发者_Go百科ou sure you want to remove Contract
Period <%= 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) + "?');";
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 + "?');" %>" />
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?
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 %>?');" />
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) %>' />
精彩评论