Anicho
Anicho

Reputation: 2667

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"
                            runat="server"
                            ErrorMessage="Some error message <%=//Do something here %>"
</asp:RequiredFieldValidator>

You can see I am using the <%= %> sign in the ErrorMessage parameter.

Upvotes: 2

Views: 84

Answers (2)

Samuel Neff
Samuel Neff

Reputation: 74919

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.

Upvotes: 3

Oded
Oded

Reputation: 499132

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";

Upvotes: 6

Related Questions