1

I have a Repeater with a TextBox and a RegularExpressionValidator (REV) on every row. I bind the validationexpression for REVs in the aspx by doing,

<asp:RegularExpressionValidator ID="regexTeam" ErrorMessage="myerrormessage" 
       ValidationGroup="ValidationGroup1" Display="None" 
       ControlToValidate="txtTeam" runat="server" 
       ValidationExpression='<%# MyNamespace.Constants.RegularExpression.myvalidationexpressionconstant %>'>
</asp:RegularExpressionValidator>

This works fine.

Now I have another place(not within the Repeater) with the same kind of TextBox and an REV. I do the same:

<asp:RegularExpressionValidator ID="regexStatus" ErrorMessage="myerrormessage" 
        ValidationGroup="ValidationGroup2" Display="None" 
        ControlToValidate="txtStatus" runat="server" 
        ValidationExpression='<%# MyNamespace.Constants.RegularExpression.myvalidationexpressionconstant %>'>
</asp:RegularExpressionValidator>

The second one does not work. It does not accept any input. In the page source, there is no regexstatus.validationexpression. The regexteam.validationexpression exists as expected. Any ideas?

PS: The constants are defined in a separate project in a struct. I cannot make it a static class.

Kelsey
  • 47,246
  • 16
  • 124
  • 162

1 Answers1

0

In the one not in the Repeater, there is no 'bind' action happening so the <%# code is not firing.

You can call a bind at the page level to cause it to fire if you want but not sure what the side effects of that would be or you can use <%= instead that just executes regardless of binding.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • "<%=" does not resolve the constant to the actual expression, right.. is that what you meant? – user3797607 Jul 08 '14 at 21:28
  • @user3797607 the `<%=` will just execute the code and write the result as the output to the html source. The `<%#` does the same thing but it is triggered by a DataBind event. Because your second control has no DataBind event occurring, the code never executes. – Kelsey Jul 08 '14 at 21:30
  • If I do, "<%= ", the page source shows regexstatus.validationexpression = MyNamespace.Constants.RegularExpression.myvalidationexpressionconstant – user3797607 Jul 08 '14 at 21:32
  • You may need to ensure whatever you are 'reading' is valid and not just outputting a string. It should execute the code as if it was run and display the result. Check out this thread: http://stackoverflow.com/questions/197047/what-is-the-difference-between-and – Kelsey Jul 08 '14 at 21:33
  • Thanks, but looks like I have no option to do it this way? <% gives "<% Mynamespace.Constants.RegularExpression.myvalidationexpressionconstant %>" :( – user3797607 Jul 08 '14 at 21:41
  • It's <%= not <% also you could store the value in your pageload event and then just put the variable in its place – Kelsey Jul 08 '14 at 21:52
  • No, <%= does not work. I just see the same "<% Mynamespace.Constants.RegularExpression.myvalidationexpressionconstant %>" – user3797607 Jul 09 '14 at 00:15
  • Then try assigning it to a global var in your PageLoad and then use that variable... There must be more going on if this does work. – Kelsey Jul 09 '14 at 00:16
  • yea..i was trying to avoid that. but looks like I don't have another option. I will just do a textstatus.databind and play around i guess. Thanks again for your time.. – user3797607 Jul 09 '14 at 00:33