Text will not change

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2982 Views - Last Post: 24 September 2012 - 09:31 AM Rate Topic: -----

#1 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Text will not change

Posted 19 September 2012 - 12:41 PM

Hi:

I'm having a small issue with my code - whenever I click submit the label in the textbox will not change to "a" and I'm not sure why.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtName.Focus();

        compDate.ValueToCompare = DateTime.Now.ToString("d");
    }

    protected void chkCoach_CheckedChanged1(object sender, EventArgs e)
    {
        txtCoach.Visible = chkCoach.Checked;
    }
    protected void chkReferee_CheckedChanged(object sender, EventArgs e)
    {
        txtReferee.Visible = chkReferee.Checked;
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txtPass.Text.Length >= 6)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError2.Text = "a";
        if (Page.IsValid)
        {
            lblResult.Text = "Thank you for your input. We will be contacting you soon.";
        }

        if (chkCoach.Checked == true)
        {
            lblError2.ForeColor = System.Drawing.Color.Red;
            lblError2.Text = "You must select an option";
        }
        int a = int.Parse(txtBirth.Text);

        if (a < 18)
        {
            lblError.ForeColor = System.Drawing.Color.Red;
            lblError.Text = "You must be at least 18 years of age.";
        }

    }
}
    


Here's the other page:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>HASC Registration</title>
    <script type= "text/javascript">

        function ClientValidate(source, args) {
            if (txtPass.Text.Length >= 6) {
                args.IsValid = true;
            } else {
            args.IsValid = false;
        }
    </script>
  
</head>
<body>
    <form id="form1" runat="server" defaultbutton="btnSubmit">
    <div>
    
        <h2>
            Hamilton Adult Soccer Club Registeration</h2>


        <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqName" runat="server" ControlToValidate="txtName" SetFocusonerror="true" ErrorMessage="Name is required">Required</asp:RequiredFieldValidator>
           
        <br />
       
        <asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqEmail" runat="server" Display="Dynamic" ControlToValidate="txtEmail" SetFocusonerror="true" ErrorMessage="Email is required">Required</asp:RequiredFieldValidator>
        &nbsp;<asp:RegularExpressionValidator ID="regEmail" runat="server" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Email is invalid" SetFocusonerror="true"></asp:RegularExpressionValidator>

        <br />

        <asp:Label ID="lblGender" runat="server" Text="Gender:"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="lstGender" runat="server" >
            <asp:ListItem Value="none">Select</asp:ListItem>
            <asp:ListItem Value="M">Male</asp:ListItem>
            <asp:ListItem Value="F">Female</asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:RequiredFieldValidator ID="reqGender" runat="server" ControlToValidate="lstGender" InitialValue="none" SetFocusonerror="true" ErrorMessage="Gender is required">Required</asp:RequiredFieldValidator>
        <br />

        <asp:Label ID="lblBirth" runat="server" Text="Date Of Birth:"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtBirth" runat="server" ></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqBirth" SetFocusonerror="true" runat="server" ControlToValidate="txtBirth" ErrorMessage="Date is required">Required</asp:RequiredFieldValidator>
        &nbsp;<asp:CompareValidator ID="compDate" runat="server" ControlToValidate="txtBirth" Type="Date" Operator="LessThan" SetFocusonerror="true"
            ErrorMessage="Date invalid" Display="Dynamic"></asp:CompareValidator>
        &nbsp;<asp:Label ID="lblError" runat="server"></asp:Label>
        <br />

        <asp:Label ID="lblAdd1" runat="server" Text="Address Line 1:"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtAdd1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqAdd" runat="server" SetFocusonerror="true" ControlToValidate="txtAdd1" ErrorMessage="Address Line 1 is required">Required</asp:RequiredFieldValidator>
    
    </div>

    <asp:Label ID="Label1" runat="server" Text="Address Line 2:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtAdd2" runat="server"></asp:TextBox>
    <br />

    <asp:Label ID="lblCity" runat="server" Text="City:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqCity" runat="server" ControlToValidate="txtCity" SetFocusonerror="true" ErrorMessage="City is required">Required</asp:RequiredFieldValidator>
    <br />

    <asp:Label ID="lblProvince" runat="server" Text="Province:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="lstProvince" runat="server">
        <asp:ListItem>AB</asp:ListItem>
        <asp:ListItem>BC</asp:ListItem>
        <asp:ListItem>MB</asp:ListItem>
        <asp:ListItem>NB</asp:ListItem>
        <asp:ListItem>NL</asp:ListItem>
        <asp:ListItem>NT</asp:ListItem>
        <asp:ListItem>NS</asp:ListItem>
        <asp:ListItem>NU</asp:ListItem>
        <asp:ListItem Selected="True">ON</asp:ListItem>
        <asp:ListItem>PE</asp:ListItem>
        <asp:ListItem>QC</asp:ListItem>
        <asp:ListItem>SK</asp:ListItem>
        <asp:ListItem>YT</asp:ListItem>
    </asp:DropDownList>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />

    <asp:Label ID="lblPostal" runat="server" Text="Postal Code:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPostal" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqPostal" runat="server" Display="Dynamic" ControlToValidate="txtPostal" SetFocusonerror="true" ErrorMessage="Postal code is required">Required</asp:RequiredFieldValidator>
    &nbsp;<asp:RegularExpressionValidator ID="regPostal" ValidationExpression="[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d" runat="server" ControlToValidate="txtPostal" ErrorMessage="Postal code invalid, format must be A9A 9A9"></asp:RegularExpressionValidator>
    <br />

    <asp:Label ID="lblPhone" runat="server" Text="Phone:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqPhone" runat="server" Display="Dynamic" ControlToValidate="txtPhone" SetFocusonerror="true" ErrorMessage="Phone number is required">Required</asp:RequiredFieldValidator>
    &nbsp;<asp:RegularExpressionValidator ID="regPhone" ValidationExpression=" ^\d{3}-\d{3}-\d{4}$" ControlToValidate="txtPhone" runat="server" ErrorMessage="Invalid phone format. Format must be xxx-xxx-xxxx"></asp:RegularExpressionValidator>
    <br />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:CheckBox ID="chkPlayer" runat="server" Text="Player" />
    <asp:CheckBox ID="chkCoach" runat="server" Text="Coach" AutoPostBack="true" oncheckedchanged="chkCoach_CheckedChanged1"  />
    <asp:CheckBox ID="chkReferee" runat="server" Text="Referee" AutoPostBack="true" oncheckedchanged="chkReferee_CheckedChanged" />
    &nbsp;<asp:Label ID="lblError2" runat="server">l</asp:Label>
    <br />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtCoach" runat="server" TextMode="MultiLine" Visible="False">Please summarize your coaching experience</asp:TextBox>
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtReferee" runat="server" TextMode="MultiLine" 
        Visible="False">Please summarize your referee experience.</asp:TextBox>
    <br />

    <br />
    <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPass" TextMode="Password" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqPassword" runat="server" ControlToValidate="txtPass" SetFocusonerror="true" ErrorMessage="Password is required">Required</asp:RequiredFieldValidator>
    &nbsp;<asp:CustomValidator ID="custPassword" runat="server" ErrorMessage="Password must be at least 6 characters" 
       ControlToValidate="txtPass" ClientValidationFunction="ClientValidate" SetFocusonerror="true" Display="Dynamic" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    <br />

    <asp:Label ID="lblConfirm" runat="server" Text="Confirm Password:"></asp:Label>
    &nbsp;
    <asp:TextBox ID="txtConfirm" TextMode="Password" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="reqConfirm" runat="server" ControlToValidate="txtConfirm" SetFocusonerror="true" ErrorMessage="Confirmation password required">Required</asp:RequiredFieldValidator>
    &nbsp;<asp:CompareValidator ID="compConfirm" Display="Dynamic" ControltoCompare="txtPass" ControlToValidate="txtConfirm" runat="server" ErrorMessage="">Password confirmation doesn't match</asp:CompareValidator>
    <br />

    <br />
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
        Text="Submit" />
    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" />
    <asp:ValidationSummary ID="valSummary" runat="server" />
    <asp:Label ID="lblResult" runat="server"></asp:Label>
    </form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Text will not change

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: Text will not change

Posted 19 September 2012 - 01:51 PM

Try adding ClientIdMode="Static" to lblError2. I didn't test that, because I don't feel like commenting out all of your validators...

EDIT: And what's with all of the <b></b>s inside the word "expression" all over the .aspx page?

This post has been edited by h4nnib4l: 19 September 2012 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: Text will not change

Posted 19 September 2012 - 02:13 PM

first, you can't reference controls in javascript like you can in your code behind. you need to use document.getElementByID('<%=txtPass.ClientID%>');

What does show up in the lblError2 when the page finishes reloading?
Was This Post Helpful? 0
  • +
  • -

#4 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Text will not change

Posted 19 September 2012 - 02:37 PM

View Posth4nnib4l, on 19 September 2012 - 01:51 PM, said:

Try adding ClientIdMode="Static" to lblError2. I didn't test that, because I don't feel like commenting out all of your validators...

EDIT: And what's with all of the <b></b>s inside the word "expression" all over the .aspx page?


I'll try that out.

Thats because I'm trying to create space between the field and the label - I just pressed tab on the design view mode. Is there a better, much more cleaner way that I could organize it?


View Postrgfirefly24, on 19 September 2012 - 02:13 PM, said:

first, you can't reference controls in javascript like you can in your code behind. you need to use document.getElementByID('<%=txtPass.ClientID%>');

What does show up in the lblError2 when the page finishes reloading?


It just continues to show up as 'l' after the submit button has been clicked
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: Text will not change

Posted 19 September 2012 - 02:51 PM

Change the javascript and see if starts working, otherwise have you debugged using Developer tools for the javascript and breakpoints for the code behind?

I took your code and ran it to see what I was getting and I see several issues:

One, if you don't know it already you will want to learn CSS and how to use divs to place things on the screen. your controls are all over the place, textboxes are not lining up like they should.

two, your phone number validator is firing even though there is a valid phone number entered.

You ask for date of birth which is typically written as MM/DD/YYYY but in the submit click event you parse it like an int. That isn't going to work. It threw an input not in correct format error for me.

Also your javascript is throwing an error aswell, first it is missing a } at the end, also it doesn't know what txtPass is.

This post has been edited by rgfirefly24: 19 September 2012 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: Text will not change

Posted 19 September 2012 - 03:20 PM

I'm talking about <asp:RegularExp<b></b>pressionValidator ValidationExp<b></b>ression.... It looks like you might have tried to do a find and replace or something, and not noticed everywhere it was added.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Text will not change

Posted 20 September 2012 - 05:38 AM

h4nnib4l, the "Expression" stuff is with the forum software. There is a bug in the forum software's code element that puts "<b></b>" between the 'p' and 'r' in 'Expression'. That's not in the OP's code.
Was This Post Helpful? 0
  • +
  • -

#8 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: Text will not change

Posted 20 September 2012 - 06:24 AM

Oh, good to know, thanks. That had me so distracted I forgot to mention the Javascript issue that rgfirefly24 posted about. I really couldn't imagine why anyone would do that...

This post has been edited by h4nnib4l: 20 September 2012 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Text will not change

Posted 23 September 2012 - 05:22 PM

Is this better? Or am I missing something more?

<script type="text/javascript">

        function ClientValidate(source, args) {

            document.getElementByID('<%=txtPass.ClientID%>');

            if (txtPass.Text.Length >= 6) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script>

Was This Post Helpful? 0
  • +
  • -

#10 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: Text will not change

Posted 24 September 2012 - 05:33 AM

no, because your still attempting to pull values directly from a textbox. Remember, javascript in CLIENT side while your controls, and code behind is SERVER side. The two do not know anything about each other.

change your javascript like so:

		<script type="text/javascript">
			function ClientValidate(source, args) {
				var password = document.getElementByID('<%=txtPass.ClientID%>');
				if (password != null) {
					if (password.length >= 6) {
						args.IsValid = true;
					} else {
						args.IsValid = false;
					}
				}
				else {
					args.IsValid = false;
				}
			}
		</script>


Was This Post Helpful? 1
  • +
  • -

#11 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Text will not change

Posted 24 September 2012 - 06:18 AM

Oh, I was so confused as to how Javascript worked vs how ASP.NET worked...thanks for clarifying!
Was This Post Helpful? 0
  • +
  • -

#12 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: Text will not change

Posted 24 September 2012 - 06:33 AM

This explains the document.GetElementById method.
Was This Post Helpful? 2
  • +
  • -

#13 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Text will not change

Posted 24 September 2012 - 06:53 AM

I'm not understanding this DoB thing...I mean how can I convert it to a string?

   int year = DateTime.Today.Year - Convert.ToDateTime(txtBirth.Text).Year;

        if (year < 18){
            lblError.ForeColor = System.Drawing.Color.Red;
            lblError.Text = "You must be 18 years or older.";
        }

Was This Post Helpful? 0
  • +
  • -

#14 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: Text will not change

Posted 24 September 2012 - 07:00 AM

Convert attempts to take a string and turn it into what ever you tell it in this case DateTime. if it can't do the conversion it will throw an unhandled error. I would suggest that you use the DateTime.TryParse method instead which will gracefully fail instead of throwing an error.


DateTime birthDate;

If(!DateTime.TryParse(txtBirth.Text, out birthDate)
{
//do something if it fails.
}



Was This Post Helpful? 0
  • +
  • -

#15 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: Text will not change

Posted 24 September 2012 - 07:48 AM

You'll make your life a lot easier if you actively ensure that users are entering proper dates into that textbox. The AjaxControlToolkit is definitely worth looking into. The MaskedEditExtender and CalendarExtender would both do the trick nicely, and it's full of other goodies that you'll soon not know how you lived without. With either of those in place, you can be pretty confident that the DateTime.TryParse will succeed (although you should still leave use it).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2