This is a migrated thread and some comments may be shown as answers.

Using a constant for an attribute

2 Answers 49 Views
Getting started with ASP.NET
This is a migrated thread and some comments may be shown as answers.
Maus
Top achievements
Rank 1
Maus asked on 02 Apr 2007, 06:30 PM
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 Answers, 1 is accepted

Sort by
0
Johan
Top achievements
Rank 1
answered on 03 Apr 2007, 02:40 PM
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";
    }
...
0
Maus
Top achievements
Rank 1
answered on 06 Apr 2007, 06:58 PM
Actually, I found that removing the "#" seems to have made it work properly.  Still, thanks for replying.
Tags
Getting started with ASP.NET
Asked by
Maus
Top achievements
Rank 1
Answers by
Johan
Top achievements
Rank 1
Maus
Top achievements
Rank 1
Share this question
or