Ajay Gautam

Ajay Gautam

  • NA
  • 204
  • 0

Second time TextBox_TextChanged event is not firing

Oct 28 2010 10:24 AM

I am generating UserName based on FirstName.LastName after that I am hitting the database and checking if this username (Like AAA.BBB) exists or not if not then I am generating email address, if yes then it is thorowing error which is correct. And after this when I changed the username (Like AAA.BBBC) and hit enter or tab then again it should check the database and generate the email but it is not doing any thing means I think I am missing the cursor control or Second time TextBox_TextChanged event is not firing.

Any idea?

My code is: -

.aspx

<th>Username </th>

  <td>

   <asp:TextBox ID="txtUsernameB" runat="server" CssClass="txtRequired" MaxLength="20"  TabIndex="4" AutoPostBack="true" ValidationGroup="vUserDetailsB" Width="243px" >  </asp:TextBox>                                

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtUsernameB" Display="Dynamic" ErrorMessage="Invalid username.  Must be between 6 and 20 characters and in non-email format." Font-Size="8pt" SetFocusOnError="True" ValidationExpression="^[a-zA-z][a-zA-Z0-9\.'_-]{4,18}[a-zA-Z0-9]$" ValidationGroup="vUserDetailsB" Width="100%"></asp:RegularExpressionValidator>            

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"  ControlToValidate="txtUsernameB"Display="Dynamic" ErrorMessage="Username is required." Font-Size="8pt" ValidationGroup="vUserDetailsB" Width="100%">Username is required.</asp:RequiredFieldValidator>

<asp:CustomValidator ID="CustomValidator2" runat="server" Display="Dynamic" ErrorMessage="The username you entered already exists in the system.  Please check if you already have an account or specify a different username." Font-Size="8pt" OnServerValidate="CvUsernameServerValidate" ValidationGroup="vUserDetailsB" Width="100%">Username already exists.&#160; Please specify a different username.</asp:CustomValidator>

</td>

.cs

protected void txtLastNameB_TextChanged(object sender, EventArgs e)

    {

if (loggedInUser.IsInternal())

        {

            if (!string.IsNullOrEmpty(txtFirstNameB.Text) && (!string.IsNullOrEmpty(txtLastNameB.Text)))

            {

                if (String.IsNullOrEmpty(txtUsernameB.Text))

                {

                    txtUsernameB.Text = txtFirstNameB.Text + '.' + txtLastNameB.Text;

                }

                RegularExpressionValidator3.Validate();

                RequiredFieldValidator4.Validate();

                CustomValidator2.Validate();

            }

        }

    }

    protected void txtUsernameB_TextChanged(object sender, EventArgs e)

    {

        if (loggedInUser.IsInternal())

        {

            if (!String.IsNullOrEmpty(txtUsernameB.Text))

            {

                if (String.IsNullOrEmpty(txtEmailB.Text))

                {

                    txtEmailB.Text = txtUsernameB.Text + Resource.DefaultUPN;

                }

                RegularExpressionValidator3.Validate();

                RequiredFieldValidator4.Validate();

                cvEmailAddress.Validate();

            }

        }

    }

protected void CvUsernameServerValidate(object source, ServerValidateEventArgs args)

    {

        var userDal = new UserDal();

        UserAccountType accountType = UserAccountType.FindAccountType(ddlAccountType.SelectedValue);

        if (accountType == null)

        {

            args.IsValid = false;

        }

        if (Keups.Business.User.IsLogonReserved(txtUsernameB.Text, accountType.DefaultActiveDirectory))

        {

            args.IsValid = false;

            CustomValidator2.Text = Resource.NewAccountUsernameExists;

            CustomValidator2.ErrorMessage =

                "The username provided already exists.  Either specify a different username or leave blank to have the system generate one for you.";

            return;

        }

        // Check to see if exists in system (including terminated)

        string logon = txtUsernameB.Text + "@" + accountType.DefaultActiveDirectory.DefaultUpnSuffix.Replace("@", String.Empty);

        //DataTable dt = userDal.FindUserByLogon(logon, true);

        Keups.Business.User testUser = Keups.Business.User.FindUser(logon, true);

        if (testUser != null)

        {

            CustomValidator2.Text = Resource.NewAccountUsernameExists;

            CustomValidator2.ErrorMessage =

                "The username provided already exists.  Either specify a different username or leave blank to have the system generate one for you.";

            args.IsValid = false;

        }

        else

        {

            args.IsValid = true;

        }

        if (loggedInUser.IsInternal())

        {

            if (!String.IsNullOrEmpty(txtUsernameB.Text))

            {

                if (String.IsNullOrEmpty(txtEmailB.Text))

                {

                    txtEmailB.Text = txtUsernameB.Text + Resource.DefaultUPN;

                }

                RegularExpressionValidator3.Validate();

                RequiredFieldValidator4.Validate();

                cvEmailAddress.Validate();

            }

        }

    }


Answers (1)