Using a constant for an attribute

3 posts, 0 answers
  1. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 02 Apr 2007 Link to this post

    I have some instances of 

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtNew" ErrorMessage="Folder path can only contain alphanumeric characters." ValidationExpression='<%# NewFolderRegEx %>' ValidationGroup="New" />

    spread throughout my project.  I set up a constant named "NewFolderRegEx" in a class that is imported by all of these pages, so that I can simply change the regular expression once, and have it apply across the board.  However, actually accessing that constant from within the control, as shown above, is messing with me.  What's the proper syntax for bringing in a variable like that?

  2. Johan
    Johan avatar
    617 posts
    Member since:
    Mar 2007

    Posted 03 Apr 2007 Link to this post

    The <%# ... %> syntax is a databinding expression syntax and refers to the current INamingContainer of the control being databinded. Since in your case the validator is not inside a control that is being databinded, the databinding expressions will not work.

    The solution would be to set the ValidationExpression property programmatically, using code in Page_Load. Example:

    ...
        protected void Page_Load(object sender, System.EventArgs e)
        {
            RegularExpressionValidator1.ValidationExpression = "some expression";
        }
    ...
  3. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 06 Apr 2007 Link to this post

    Actually, I found that removing the "#" seems to have made it work properly.  Still, thanks for replying.
Back to Top