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

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.

Related

ASP.NET Javascript runtime error

I have just started a new ASP.NET web application and I am totally new to everything and after moving a few things round I have been getting this error and I don't understand what it means.
It happens when I go to enter anything in the Password box.
Could someone please help me out on this issue.
EDIT:
<li>
<asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" onkeypress="capLock(event)" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
Try to do that inside
window.onload = function(){
// code goes here
}
I think you are trying to access DOM even before it is being created.
the document.getElementById('divMayus') is undefined
document.getElementById("divMayus") is returning null or undefined.
You should look at the rendered html and find the div that you are trying to reference to make sure the id is the same as you are trying to reach. Asp.net alters the ids of server controls depending on their containers. So if you moved stuff around, the id may have also been changed.

Issue related to lost focus in Textbox when RequiredValidator is used

I have an asp.net Web Application, i have login screen in Application.
In userName and password field i have used required field validator as follows :
<td style="width: 160px">
<asp:TextBox ID="txtUserNm" runat="server" CssClass="txtSingleline txtBack-Color txtRequireBorder-Color required" Height="18px" Width="150px" TabIndex="1" MaxLength="50" onblur="ValidatorOnChange(event);showhide();"></asp:TextBox>
</td>
<td style="width: 140px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
Width="150px" SetFocusOnError="true" ErrorMessage="Please enter Username" ControlToValidate="txtUserNm" ValidationGroup="a"></asp:RequiredFieldValidator>
when i focus on UserName textbox and Press Tab , i am unable to loos the focus from the TextBox because of RequiredField Validator. it happens only in Chrome and IE9 . but working well in firefox.
what can be solution to loose the focus from the Textbox when using Requiredfield validator ???
Thanks
Simple... just remove the SetFocusOnError="true" or set it to false.
The only downside is you can only validate the form on form post back, not on the fly.
But, it's not even such a pain in the neck, since even if you remove this property, validation works fine in both normal web forms, and ajax update panels.
I assume that you should manually assign tabindex property to your controls in order to define loop by pressing tab.

Get username from createuserwizard to add to role

I'm trying to build some simple user controls for a standard ASP.NET membership system. I have a CreateUserWizard control which, when submitted needs to add the user to a particular role.
The ascx file looks like this:
<asp:CreateUserWizard ID="CreateUserWizard2" runat="server" oncreateduser="CreateUserWizard2_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizard2Step1" runat="server">
<ContentTemplate>
<div class="form-error">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</div>
<fieldset class="member-control">
<legend>Registration details</legend>
<div class="standard">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username*:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="text-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="Username is required." ToolTip="Username is required." ValidationGroup="CreateUserWizard1">Username is required.</asp:RequiredFieldValidator>
</div> ...etc
The C# code-behind looks like this:
namespace DocumentMembership
{
public partial class ClientRegister : System.Web.UI.UserControl
{
protected void CreateUserWizard2_CreatedUser(object sender, EventArgs e)
{
// add member to role
Roles.AddUserToRole(this.CreateUserWizard2.UserName, "Client");
However, I'm getting a build error saying:
'DocumentMembership.ClientRegister' does not contain a definition for 'CreateUserWizard2' and no extension method 'CreateUserWizard2' accepting a first argument of type 'DocumentMembership.ClientRegister' could be found (are you missing a using directive or an assembly reference?)
When I begin typing Roles.AddUserToRole(this. intellisense doesn't seem to give anything useful to be able to get the username from the form.
I'm really not very skilled at .NET so it's probably something obvious I'm doing wrong, but I just can't seem to find a fix.
Can anyone suggest how to get this working or what might be the issue?
Thanks!
you can try with this code
CreateUserWizard2.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
Got it. It was indeed something silly. The ascx file was inheriting from the wrong code-behind, hence it wasn't recognising any method of retrieving the username from the form.
if it's about the role assigning try this :
Roles.AddUserToRole((sender as CreateUserWizard).UserName, "LC");

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

Read validation message from external library

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;

Categories