Click here to Skip to main content
11,412,345 members (63,175 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
In my following code whenever I am entering value in my password textbox (i.e txtPassword) the value gets erased when i my entering value in next textbox.
I debugged my code but still not getting the error.

protected void Page_Load(object sender, EventArgs e)
    {
        txtSecQue.Visible = false;
        lblUser.Visible = false;
        //txtUsername.Focus();
        lblEmailID.Visible = false;
        lblQue.Visible = false;
        lblPassword.Visible = false;
    }
    protected void ddlSecQue_SelectedIndexChanged(object sender, EventArgs e)
    {
        
            if (ddlSecQue.SelectedItem.Text == "Other")
            {
                txtSecQue.Visible = true;
                txtSecQue.Focus();
            }
            else if (ddlSecQue.SelectedItem.Text=="----Select----")
            {
                lblQue.Visible = true;
            }
            else
            {
                txtAnswer.Focus();
            }
        
    }
    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
        //to check length of the string
        string username=txtUsername.Text;
        if (username.Length<5 || username.Length>15)
        {
            lblUser.Text = "Username must be between 5 to 15 characters";
            lblUser.Visible = true;
            txtUsername.Focus();
            txtUsername.Text = "";
        }
        else
        {
            txtPassword.Focus();
        }
 
        string select = "Select * from Reg_Info where Username='"+txtUsername.Text+"'";
        SqlCommand cmd = new SqlCommand(select, con);
        SqlDataReader sdr;
 
        con.Open();
        sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            lblUser.Visible = true;
            txtUsername.Text = "";
            txtUsername.Focus();
        }
        else
        {
            txtPassword.Focus();
        }
        con.Close();
    }
    protected void txtEmailID_TextChanged(object sender, EventArgs e)
    {
       
        string select = "Select * from Reg_Info where EmailId='" + txtEmailID.Text + "'";
        SqlCommand cmd = new SqlCommand(select, con);
        SqlDataReader sdr;
 
        con.Open();
        sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            lblEmailID.Visible = true;
            txtEmailID.Text = "";
            txtEmailID.Focus();
        }
        con.Close();
    }
    protected void txtPassword_TextChanged(object sender, EventArgs e)
    {
        string password = txtPassword.Text;
        if (password.Length<7 || password.Length>15)
        {
            lblPassword.Visible = true;
            txtPassword.Text = "";
            txtPassword.Focus();
        }
        else
        {
            txtRePassword.Focus();
        }
    }

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Registeration.aspx.cs" Inherits="Registeration" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style2
        {
            height: 23px;
        }
        .style3
        {
            height: 23px;
            width: 252px;
        }
        .style4
        {
            width: 252px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:TabContainer ID="TabContainer1" runat="server" Height="500" Width="1000" 
        ActiveTabIndex="0">
    <asp:TabPanel ID="TabLogin" runat="server">
    <HeaderTemplate>Login Details</HeaderTemplate>
        <ContentTemplate><table class="style1"><tr><td class="style3">Username :</td><td class="style2"><asp:TextBox 
                ID="txtUsername" runat="server" AutoPostBack="True" 
                ontextchanged="txtUsername_TextChanged"></asp:TextBox> <asp:RequiredFieldValidator 
                ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtUsername" 
                ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
             <asp:Label ID="lblUser" runat="server" Text="Username already exist"></asp:Label>
            </td></tr><tr><td class="style4">Password :</td><td><asp:TextBox ID="txtPassword" 
                    runat="server" TextMode="Password" AutoPostBack="True" 
                    ontextchanged="txtPassword_TextChanged"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txtPassword" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:Label ID="lblPassword" runat="server" Text="5 to 15"></asp:Label>
                </td></tr><tr><td class="style4">Re-Type Password :</td><td><asp:TextBox ID="txtRePassword" runat="server" TextMode="Password"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ControlToValidate="txtRePassword" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:CompareValidator ID="CompareValidator1" runat="server" 
                    ControlToCompare="txtPassword" ControlToValidate="txtRePassword"
                    ErrorMessage="CompareValidator"></asp:CompareValidator>
                </td></tr><tr><td class="style4">Email ID :</td><td><asp:TextBox 
                ID="txtEmailID" runat="server" Width="200px" AutoPostBack="True" 
                    ontextchanged="txtEmailID_TextChanged"></asp:TextBox> <asp:RequiredFieldValidator 
                    ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtEmailID" 
                    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                    runat="server" ControlToValidate="txtEmailID" 
                    ErrorMessage="RegularExpressionValidator" 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                 <asp:Label ID="lblEmailID" runat="server" Text="EmailID already exist"></asp:Label>
                </td></tr><tr><td class="style4">Security Question :</td><td><asp:DropDownList 
                    ID="ddlSecQue" runat="server" 
                            onselectedindexchanged="ddlSecQue_SelectedIndexChanged" 
                    AutoPostBack="True" ><asp:ListItem>----Select----</asp:ListItem><asp:ListItem>Where you where born</asp:ListItem><asp:ListItem>What is your cousin name ?</asp:ListItem><asp:ListItem>What is your first grade teacher name ? </asp:ListItem><asp:ListItem>Other</asp:ListItem><asp:ListItem></asp:ListItem></asp:DropDownList>  <asp:TextBox ID="txtSecQue" runat="server"></asp:TextBox> <asp:Label 
                    ID="lblQue" runat="server" Text="Please Select one option"></asp:Label>
                </td></tr><tr><td class="style4">Answer :</td><td><asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                    ControlToValidate="txtAnswer" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                </td></tr><tr><td colspan="2">               <asp:Button ID="btnReg" runat="server" Text="Register" />   <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td></tr></table></ContentTemplate>
    </asp:TabPanel>
 
    <asp:TabPanel ID="TabPersonalInfo" runat="server">
    <HeaderTemplate>Personal Information</HeaderTemplate>
        <ContentTemplate><table class="style1"><tr><td>First Name :</td><td> </td></tr><tr><td>Last Name :</td><td> </td></tr><tr><td>Address Street1 :</td><td> </td></tr><tr><td>Address Street2 :</td><td> </td></tr><tr><td>City :</td><td> </td></tr><tr><td>State :</td><td> </td></tr><tr><td>Date of Birth :</td><td> </td></tr><tr><td>Mobile No :</td><td> </td></tr><tr><td>Alternate No :</td><td> </td></tr><tr><td>Gender :</td><td> </td></tr><tr><td 
                colspan="2">         <asp:Button ID="btnReg0" runat="server" 
                Text="Register" />  <asp:Button ID="btnCancel0" runat="server" 
                Text="Cancel" /></td></tr></table></ContentTemplate>
    </asp:TabPanel>
    </asp:TabContainer>
</asp:Content>

I am using autoPostback to check each textbox
Tell me where I am going wrong

Thank You
Posted 2-Apr-13 5:33am
NeenaM1.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are setting the txtPassword to an empty string on the TextChanged Event. That is why it is blanking out. It appears you are doing that with all of the text fields.
  Permalink  
v2
Comments
NeenaM at 2-Apr-13 11:42am
   
But even if I am not using txtPassword.text="" I am facing the same error
I am facing this problem only for Password text field and not for all text field
richcb at 2-Apr-13 11:48am
   
I believe aspnet_regiis addresses that in the solution below. The browser clears all password fields on a postback for security reasons.
NeenaM at 2-Apr-13 12:20pm
   
ok understood
I am using the below solution
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

When you enter the value in txtEmailID and move the mouse out , the page postbacks because you have set the TextChanged event and the Autopostback=true for this textbox. And passwords are cleared by the browser on postbacks for security reasons...


And why in the world , you have password fields before email ID ???
  Permalink  
Comments
NeenaM at 2-Apr-13 12:23pm
   
Thanks
Can you tell me why should one not use email ID before password
aspnet_regiis -i at 2-Apr-13 12:27pm
   
In your scenario specifically, the passwords are lost due to postback.. So place the controls which need to postback (email) BEFORE the password controls and hope that the user fills them in sequence...
NeenaM at 2-Apr-13 14:34pm
   
ok
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

ur checking password strength on server side by textchange event that's why autopostback is ocured except that add RegularExpressionValidator
for example
<asp:RegularExpressionValidator ID="revPassCheck" runat="server" ErrorMessage="Password lenght must be between 6 to 15 characters."
            ControlToValidate="txtPassword" ValidationExpression="[\s\S]{7,15}"></asp:RegularExpressionValidator>
  Permalink  
Comments
NeenaM at 2-Apr-13 12:19pm
   
Thanks I will do the same :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 332
1 Sascha Lefévre 200
2 Maciej Los 135
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 115
0 Sergey Alexandrovich Kryukov 8,980
1 OriginalGriff 7,263
2 Maciej Los 3,500
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100