Read validation message from external library - c#

I created a Global Resource file for Error messages and I am attaching the associated message to the validator control as following.
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
ErrorMessage="<%$ Resources:ErrorMessage, RequiredFieldTestKey %>"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>
This is working fine.
Now I am planning to move all the Global Resources to a different library let say MyResourceLibrary and created a resource file with name ErrorMessage. I have added the reference of the library to my ASP.NET project and trying to acces the message from my library as follwong.
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
ErrorMessage="<%$ Resources: MyResourceLibrary.ErrorMessage, RequiredFieldTestKey %>"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>
But this is not working.

If your resource file is named the same as the page you are on (i.e. Default.aspx.resx)
You can add meta:resourcekey as so:
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
meta:resourcekey="RequiredFieldTestKey"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>

Finally, I did this using server side code like follwong.
RVTest.ErrorMessage =MyLibrary.ValidationMessages.RequiredField;

Related

Use resources in Class Library from WebForms app

In my application there is one class library project and one website. I created a resource file in a class library and modifier of it is public. I need to use this resource in website layer:
<asp:Label ID="Label2" runat="server" Text="<%$ Resources:resourceName, ErrorEmptyName %>"></asp:Label>
But an error is shown:
The resource object with key 'ErrorEmptyName' was not found.
But in code-behind I access this key:
Label1.Text = resourceName.ErrorEmptyName;
and no error.
I find this page but i can not use:
Referencing resource files from multiple projects in a solution
What is wrong? How to do it?
You can use the code
<asp:Label ID="Label2" runat="server" Text="<%# resourceName.ErrorEmptyName %>"></asp:Label>
maybe you need to import your namespace.

"The name does not exist in the current context" in ASPX page

I've had a look at similar questions (1,2) but they don't appear to provide me a solution :S
I'm trying to add an extra field to a registration form that is already pre-built (i.e. comes with) in ASP.Net Web Form.
What I'm doing is, in the Register.aspx.cs backend I try to obtain the value txtCompanyInput.Text();:
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
... // some code here
String CompanyName = txtCompanyInput.Text();
... // other code here
Response.Redirect(continueUrl);
}
The Register.aspx itself looks like so:
...
<li>
<asp:Label runat="server" AssociatedControlID="ConfirmPassword">
Confirm password</asp:Label>
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic"
ErrorMessage="The confirm password field is required." />
<asp:CompareValidator runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic"
ErrorMessage="The password and confirmation password do not match." />
</li>
<li>
<asp:TextBox ID="txtCompanyInput" runat="server"></asp:TextBox>
</li>
...
When I try to build the project I run into an error saying:
Error 1 The name 'txtCompanyInput' does not exist in the current context c:\users\...\Account\Register.aspx.cs 62 34 ProjectTest
Is there a reason why I'm not able to reference the txtCompanyName field in the backend?
If you are using VS2013 update 5 like me, do these steps to regenerate the designer file:
Delete the stated Register.designer.cs
Highlight 'Register.aspx' (must do!)
Go Menu->'Project', Click 'Convert to Web Application'
Right click on anything in the solution explorer will NOT give you choice to 'Convert to web application'.
Hope this helps some beginners.
Ehsan Sajjad was on the right track. The Register.aspx.designer.cs file was not getting updated.
I've finally figured out this was due to the Textboxs and Labels being placed inside the asp:CreateUserWizard block - which you shouldn't do. Even after manually adding the elements to the designer.cs file would throw an error when referencing the Textbox (Object reference not set to an instance of an object)
I should have included a bigger code segment from the Register.aspx file in my initial question.

How to create custom control tool for textbox validation?

I want to create a tool like textbox validation so how to create textbox validate tool in asp.net c#
<asp:Textbox id="txtLastName" runat="server"></asp:Textbox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="txtLastName"
ErrorMessage="Last name is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

Webpage calls onclick handler even after validator is fired

We've got a web application written with VS2005, running on an ASP.NET 2.0 platform, which has a problem.
This is the code:
<td>
<asp:ImageButton ID="ibInsertEmpty_4" runat="server" ImageUrl="~/Images/ok_green.png"
OnClick="gv_mwl_EmptyInsert" />
</td>
<td>
<asp:TextBox ID="emptyMWL_ID" runat="server" Width="6em"></asp:TextBox>
<asp:RequiredFieldValidator ID="valemptyMWL_ID" runat="server"
ControlToValidate="emptyMWL_ID" ErrorMessage="<br>Link ID must not be empty!"
SetFocusOnError="true" display="Dynamic" />
</td>
which is suppose to show an error if the user clicks the button when the textbox is empty.
Well, that happens, but even after the error is shown, it still calls the onclick handler gv_mwl_EmptyInsert, as if there was no error.
Why is this happening?
Unfortunately, I can't post a link to the website (because it's on an intranet, and it uses a database which requires authorisation), but I tried to make a small testcase which has the same problem, without success. So I don't know what to do now.
Add
ValidationGroup="SomeValidationGroup"
To both the Button, and the validator

CausesValidation is set to "False" but the client side validation is still firing

I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on the client side when I press the Cancel button, which has the CausesValidation attribute set to "False". How can I get this to stop?
I do not believe that Validation Groups are supported in 1.1.
Here's a code sample:
<asp:TextBox id="UsernameTextBox" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="UsernameTextBoxRequiredfieldvalidator" ControlToValidate="UsernameTextBox"
runat="server" ErrorMessage="This field is required."></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="UsernameTextBoxRegExValidator" runat="server" ControlToValidate="UsernameTextBox"
Display="Dynamic" ErrorMessage="Please specify a valid username (6 to 32 alphanumeric characters)."
ValidationExpression="[0-9,a-z,A-Z, ]{6,32}"></asp:RegularExpressionValidator>
<asp:Button CssClass="btn" id="addUserButton" runat="server" Text="Add User"></asp:Button>
<asp:Button CssClass="btn" id="cancelButton" runat="server" Text="Cancel" CausesValidation="False"></asp:Button>
Update: There was some dynamic page generating going on in the code behind that must have been messing it up, because when I cleaned that up it started working.
Validation Groups were not added to ASP.NET until version 2.0. This is a 1.1 question.
Double check your setting and make sure you are not overwriting it in the code behind.
Are they in separate validation groups (the button and validator controls)?
You're not manually calling the JS to do the client validation are you?

Categories