Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 797
Anicho Avatar asked Feb 18 '26 21:02

Anicho


2 Answers

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";
like image 130
Oded Avatar answered Feb 21 '26 10:02

Oded


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.

like image 21
Samuel Neff Avatar answered Feb 21 '26 09:02

Samuel Neff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!