ASP.NET Javascript runtime error - c#

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.

Related

Why does textmode=password causes issue in asp.net?

I am facing a very peculiar issue.
I have set textmode of an asp.net textbox to Password but it causes a problem i.e. it picks other passwords from cookies even it has nothing to do with my page.
And setting it to password also affects my another textbox with is set to SingleLine mode but when I remove Password textmode then it works correctly.
<asp:TextBox ID="txtPassword1" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
<asp:TextBox ID="txtUserLogin1" TextMode="SingleLine" runat="server" CssClass="form-control"></asp:TextBox>
Regarding this
it picks other passwords from cookies even it has nothing to do with my page.
I think it a browser functionality which automatically populates saved password for you. So when you have type as "password", it shows you some password. Try to clear saved password and then you will see blank textbox.
I am not sure about the second issue.
I added dummy text box
<asp:TextBox ID="dummy" runat="server" style="display:none"></asp:TextBox>
under the textbox which was showing prefilled data from cache. It solved my problem.
Try to use type="Password" instead of TextMode="Password"

"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.

Javascript using Google Analytics returning Null Value, data not being captured

I am trying to capture google analytics data for a textbox where a user is typing in a zip code and then clicking the button to search a database. The application is written in C# and here is the code for the button piece:
<p></p>
<p>
<asp:Panel DefaultButton="btnSalesRepSearch" runat="server">
<asp:TextBox ID="txtZipCode" runat="server" ClientIDMode="Static" Width="300px"></asp:TextBox>
<asp:Button ID="btnSalesRepSearch" runat="server" ClientIDMode="Static" CssClass="buttonregular" Text="Search" OnClick="btnSalesRepSearch_Click" />
<asp:RequiredFieldValidator ID="reqTxtZipCode" runat="server" ValidationGroup="ZipCode" ControlToValidate="txtZipCode" Display="Dynamic" ErrorMessage="Please enter a valid US Zip code" CssClass="error"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regExTxtZipCode" runat="server" ValidationExpression="^\d{5}(-\d{4})?$" ValidationGroup="ZipCode" ControlToValidate="txtZipCode" Display="Dynamic" ErrorMessage="Please enter a valid US Zip code" CssClass="error"></asp:RegularExpressionValidator>
</asp:Panel>
</p>
I am trying to use Javascript to trigger a google analytics event that captures the data from the textbox, the problem is on Page Load I get an error saying that the submit button is Null and nothing will get captured into Google Analytics. Here is the Javascript that I am using below. Any help would be greatly appreciated.
<script type="text/javascript">
var textbox = document.getElementById('txtZipCode'),
submitButton = document.getElementById('btnSalesRepSearch');
submitButton.onclick = function (txtZipCode) {
_trackEvent('data-store', textbox.name, textbox.value, 0);
// 'data-store' can be replaced with whatever category of data you want, for sortability's sake
// the 0 can be replaced with any other numerical data you want - but it must be numerical
}
</script>
The ASP server side ids like txtZipCode are not the same as the id that is generated on the client side. Do a view source of your html and you can verify that the ids are different. Then change your document.getElementById selectors accordingly.

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

Efficient/Easy method to validate for empty text boxes?

I have a about 6 or 7 text boxes which needs to be validated to make sure they are not empty from server side code. So how I usually do this is, check each textbox 1 by 1 to ensure that they are not empty. Are there any other efficient methods to do this? I have searched on SO and have found out that adding all the textboxes to a list and using a for each is a much better method. Are there any other ways that this can be achieved? Thanx a lot in advance :)
Just check them each individually:
if (string.IsNullOrEmpty(this.NameTextBox.Text) ||
string.IsNullOrEmpty(this.AddressLine1TextBox.Text) ||
// etc...
)
{
// Handle me
}
Or possibly:
void CheckTextBox(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
if (string.IsNullOrEmpty(textBox.Text))
{
// Handle me
}
}
void Validate()
{
CheckTextBox(this.FirstNameTextBox);
CheckTextBox(this.AddressLine1TextBox);
CheckTextBox(this.AddressLine2TextBox);
}
7 text boxes really isn't that many - explicitly checking each one keeps it simple and makes sure that others reading your code know what's going on, whereas messing around with collections is adding another layer of indirection, and makes it just slightly less straightforward to debug.
Keep it simple!
I agree with Kragen - your code may look "big" because of all the checks, but you are really writing exactly what the program needs to do in order to validate these things, so any sort of clever approach that reduces the number of lines of code you write isn't actually going to speed things up that much.
Question though: do you have to validate the textbox on the server? If you are only validating that the textbox isn't empty, I'd suggest using client side validation. That will save you server time and bandwidth, since your user won't be allowed to submit the form to your server until their browser has validated that they aren't empty.
You'd still want to validate on the server side (in case they don't have JavaScript enabled on their browser or they are attempting some kind of malicious behaviour).
The native ASP.NET way of client side validation involves adding an ASP.NET validation tag to your ASPX. It's actually quite easy. Here's an example on MSDN:
http://msdn.microsoft.com/en-us/library/aa479013.aspx#aspnet-validateaspnetservercontrols_topic3
I've simplified their code a bit to match your requirements:
<form runat="server">
<asp:TextBox id="TextBox1" runat="server" />
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Name is required!" ControlToValidate="TextBox1" />
<asp:TextBox id="TextBox2" runat="server" />
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Address is required!" ControlToValidate="TextBox2" />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</form>

Categories