Reputation: 16724
I'm working on small web form,in C#/ASP.NET. I want to store the errors messages into variables of C# class; something like this:
public static class ErrorMessages {
public static string fooX = "baa";
// etc..
}
And then concat it in some attribute of an ASP
tag:
<asp:RegularExpressionValidator runat="server"
ControlToValidate="baain"
ErrorMessage= *ErrorMessages.fooX*
/>
Is it possible? if yes, how to do this?
Upvotes: 2
Views: 2068
Reputation: 223257
In the aspx page you may try:
ErrorMessage='<%# ErrorMessages.fooX %>'
In your code behind on page load call DataBind();
:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}
I am not sure about concatenating the aspx part of the page, you can set the ErrorMessage property of your RegularExpressionValidator in the code behind. Something like:
RegularExpressionValidator1.ErrorMessage = "Your message";
or
RegularExpressionValidator1.ErrorMessage = ErrorMessages.fooX;
Upvotes: 1
Reputation: 21365
This is simple, have you tried this:
<%= YourNamespace.YourConstant %>
Or (.Net 4, this option will automatically encode the text)
<%: YourNamespace.YourConstant %>
There are several operators:
<%#
Used for data binding
<%$
Used for expressions, not code; often seen with DataSources:
<%=
Returns a string
<%:
Same as <%=
but it auto-html-encodes the value within the tag
Well I just tested and it appears that you cannot use inline code with server controls...
So alternative approaches:
Simply assign the value using code behind
this.myRegularExpressionValidator.ErrorMessage = ErrorMessages.Foo;
Use resources instead. With resources you will gain many benefits like localization for your site and since resources are simply xml files, you can drop them in your site without recompile (assuming you are not using satellite assemblies to store your resources)
<asp:RegularExpressionValidator runat="server" ErrorMessage="<%$ Resources:Resource, MyResource %>" />
Where
Resource
refers to the global resource fileMyResource
refers to the key in the resource file containing the textThe global resources must be placed in the App_GlobalResources
ASP.Net special folder, as an example the resource file name: Resource.resx
Example of the resource file
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="MyResource" xml:space="preserve">
<value>My error message</value>
</data>
</root>
The resource files can be added and managed using Visual Studio so you do not have to deal manually with the XML
Upvotes: 1
Reputation: 1459
One approach to this, is to create your own custom expression builder.
The ExpressionBuilder class is the base class for expression builders, such as the AppSettingsExpressionBuilder class, that create code expressions during page parsing.
Expression builders parse declarative expressions and create code to retrieve values bound to a control property. In no-compile scenarios, an expression builder that supports a no-compile feature evaluates the expression during run time.
Please see the link from msdn for more detailed explanation and example.
http://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder.aspx
Upvotes: 1