coder
coder

Reputation: 13250

Instant check username availability in Asp.Net using ajax

I have gone through this article by Encosia and this article by Travis and another great validator from Mosa

But here in the link and in many other articles throughout the web I have seen and they were not using user Create user wizard for checking username availability. But I want to validated it using create user wizard?

Is there any possibility to convert this to get work with Create User Wizard?If so how can I do that?

Actually in Encosia's example there is a code block which shows for Username_changed:

protected void Username_Changed(object sender, EventArgs e)
{
  if (Membership.GetUser(Username.Text) != null)
    // Display a username taken message.
  else
    // Display a username available message.
}

So for this how do I change according to user wizard?As in create user wizard there is no specific change event for idividual textboxes?Here I'm little bit confused :( and I got one clarification with the Client ID

and I tried to modify it using javasript as

var UserName = '<%= ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>'; 

But this is not working.

Upvotes: 1

Views: 2116

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460098

From MSDN

Add an <asp:WizardStep> element to the <WizardSteps> section of the CreateUserWizard control. Include any controls and markup in the additional wizard step that your customized CreateUserWizard control will use.

The following code example shows a step to be added before the CreateUserStep of the CreateUserWizard control that includes a textbox control for users to enter a user name. The user name will be checked to ensure that it does not already exist in the membership database.

<asp:WizardStep ID="CreateUserWizardStep0" runat="server">
     <table border="0" style="font-size: 100%; font-family: Verdana" id="TABLE1" >
          <tr>
              <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d">
                  Select an Account Name</td>
          </tr>
          <tr>
              <td>
                <asp:Label ID="AccountNameLabel" runat="server" AssociatedControlID="SearchAccount" > 
                  Account Name:</asp:Label>
                <asp:TextBox ID="SearchAccount" runat="server"></asp:TextBox><br />
                <asp:Label ID="SearchAccountMessage" runat="server" ForeColor="red" />                                          
              </td>
          </tr>
      </table>
 </asp:WizardStep>

Add code for your wizard step. You can handle the NextButtonClick event of the Wizard control to execute your code. The CurrentStepIndex property value indicates which additional wizard step raised the NextButtonClick event by the step index number (starting from 0 for the first step).

The following code example shows a handler for the NextButtonClick event that takes the user name entered in the TextBox control in the wizard step from the previous code example and verifies that the user name is not blank and does not currently exist in the membership database. You will need to add an OnNextButtonClick attribute to the CreateUserWizard control on your page that references the handler for the NextButtonClick event handler (for example, OnNextButtonClick="CreateUserWizard1_NextButtonClick".)

private bool UserExists(string username)
{
    if (Membership.GetUser(username) != null) { return true; }

    return false;
}

protected void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
    if (e.CurrentStepIndex == 0)
    {
        if (SearchAccount.Text.Trim() == "" || UserExists(SearchAccount.Text))
        {
            SearchAccountMessage.Text = "That account already exists. Please select an different account name.";
            e.Cancel = true;
        }
        else
        {
            TextBox userName =
              (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
            userName.Text = SearchAccount.Text;
            SearchAccountMessage.Text = "";
            e.Cancel = false;
        }
    }
}

Upvotes: 1

Related Questions