Using the <% %> inside a control's parameters
I wish to do the following but it doesn't seem possible, was wondering if there is a work around or a way to make it work.
<asp:RequiredFieldValidator ID="rfvImCool"
开发者_Go百科 runat="server"
ErrorMessage="Some error message <%=//Do something here %>"
</asp:RequiredFieldValidator>
You can see I am using the <%= %>
sign in the ErrorMessage parameter.
Since <%=%>
is short for:
<script runat="server">
Response.Write();
</script>
You should be able to see why you can't use them within a server side control.
You should set the property in the code-behind page:
rfvImCool.ErrorMessage = "Some error message " + " Do something here";
You can't use <%= %>
but you can use the databinding expression <%# %>
like this:
<asp:RequiredFieldValidator ID="rfvImCool"
runat="server"
ErrorMessage="Some error message <%# SomePropertyOrEvalCall %>"
</asp:RequiredFieldValidator>
The contents of <%# %>
will get called when the control's DataBind
method is called (typically you would call this on the Page
and it would propagate down to the child controls.
精彩评论