locked
Membership.CreateUser firing twice due to Ajax Toolkit RRS feed

  • Question

  • User940894612 posted

    Hi

    I am having an issue whereby I have a custom create new user page inside an update panel.  When I press the button to call the following method

    protected void SignHimUp(object sender, EventArgs e)
        {
            MembershipCreateStatus status;
    
            try
            {
                MembershipUser createuser = Membership.CreateUser(txtUsername.Value, txtPassword.Value, txtEmailAddress.Value, txtMembershipQuestion.Value, txtMembershipAnswer.Value, true, out status);
    
                if (createuser == null)
                {
                    Error.Visible = true;
                    Error.Text = GetErrorMessage(status);
                }
                else
                {
                    Roles.AddUserToRole(txtUsername.Value, "Normal");
    
                    // Get the UserId of the just-added user
                    MembershipUser newuser = Membership.GetUser(txtUsername.Value);
                    Guid newUserId = (Guid)newuser.ProviderUserKey;
    
                    user.UserID = newUserId;
                    user.EmailAddress = txtEmailAddress.Value;
                    //TextBox EmailAddressStep2 = (TextBox)CreateUserWizard.FindControl("EmailAddress");
    
                    int AddUserDetails = userbll.Save(user);
    
                    if (AddUserDetails == 0)
                    {
                        step1.Style.Add("display", "none");
                        step2.Style.Add("display", "block");
                        Step2Head.Attributes.Remove("disabled");
                        txtFirstName.Attributes.Add("Required", "Required");
                        txtLastName.Attributes.Add("Required", "Required");
                        txtAddressLine1.Attributes.Add("Required", "Required");
                        txtCity.Attributes.Add("Required", "Required");
                        txtPostCode.Attributes.Add("Required", "Required");
                        txtPhoneNumber.Attributes.Add("Required", "Required");
                        //lblSuccess.Text = "User account created successfully";
                        txtEmailAddressConfirm.Value = user.EmailAddress;
                    }
                    else
                    {
                        Response.Redirect("Error.aspx");
                    }
                }
            }
            catch
            {
                Error.Visible = true;
                Error.Text = "An exception occurred creating the user. Please email system administrator";
            }
        }

    During debugging - the 

    MembershipUser createuser = Membership.CreateUser(txtUsername.Value, txtPassword.Value, txtEmailAddress.Value, txtMembershipQuestion.Value, txtMembershipAnswer.Value, true, out status);  is firing twice causing me to see a message saying the 'User Already Exists'.  

    Here is the code from the front-end

    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
         <script type="text/javascript">
             Sys.Application.add_load(BindEvents);
         </script>
        <div class="container-fluid">
                <div class="row">
                    <div class="col-md-3 col-sm-1 col-xs-1"></div>
                    <div class="col-md-6 col-sm-10 col-xs-10">
                        <div class="stepwizard">
                            <div class="stepwizard-row setup-panel">
                                <div class="stepwizard-step">
                                    <a href="#step-1" type="button" runat="server" clientidmode="Static" id="Step1Head" class="btn btn-primary btn-circle">1</a>
                                    <p>Registration</p>
                                </div>
                                <div class="stepwizard-step">
                                    <a href="#step-2" type="button" runat="server" clientidmode="Static" id="Step2Head" class="btn btn-default btn-circle" disabled="disabled">2</a>
                                    <p>Personal Details</p>
                                </div>
                                <div class="stepwizard-step">
                                    <a href="#step-3" type="button" runat="server" clientidmode="Static" id="Step3Head" class="btn btn-default btn-circle" disabled="disabled">3</a>
                                    <p>Sign Up</p>
                                </div>
                            </div>
                        </div>
                    </div>                    
                    <div class="col-md-3 col-sm-1 col-xs-1"></div>
                </div>
                
                <form>
                    <div class="row setup-content" id="step1" runat="server" clientidmode="Static">
                        <div class="col-md-3 col-sm-1 col-xs-1"></div>
                        <div class="col-md-6 col-sm-10 col-xs-10">
                            <br />
                            <h3 class="textaligncenter">Registration</h3>
                            <p class="textaligncenter"><b>Please complete the following form with your latest details</b></p>
                            <div class="form-group">
                                <ul class="errorMessages"></ul>
                                <label class="control-label"></label>
                                <input  maxlength="15" type="text" pattern=".{6,15}" title="Username field must be between 6-15 characters" required="required" id="txtUsername" name="UserName1" runat="server" class="TextBox100" tabindex="1" placeholder="Enter Username (6-15 characters)" onkeyup="CheckUsername(); validateForm()" onblur="CheckUsername(); validateForm()" clientidmode="Static" />
                                <div id="ValidUsername" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="50" required="required" id="txtEmailAddress" novalidate runat="server" class="TextBox100" tabindex="2" placeholder="Enter Email Address" onkeyup="validateEmail(); validateForm()" onblur="validateEmail(); validateForm()" clientidmode="Static" />
                                <div id="ValidEmail" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="15" type="password" pattern=".{6,15}" required="required" id="txtPassword" runat="server" class="TextBox100" tabindex="3" placeholder="Enter Password (6-15 characters)" onkeyup="checkVal(); CheckPasswordLength(); validateForm()" onblur="checkVal(); CheckPasswordLength(); validateForm()" clientidmode="Static" />
                                <div id="Complexity" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="15" type="password" pattern=".{6,15}" required="required" id="txtConfirmPassword" runat="server" class="TextBox100" tabindex="4" placeholder="Re-Enter Password" onkeyup="ComparePassword(); validateForm()" onblur="ComparePassword(); validateForm()" clientidmode="Static" />
                                <div id="Compare" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" required="required" id="txtMembershipQuestion" runat="server" class="TextBox100" tabindex="5" onkeyup="checkquestionlength(); validateForm()" onblur="checkquestionlength(); validateForm()" placeholder="Security Question" clientidmode="Static" />
                                <div id="ValidMembershipQuestion" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" required="required" id="txtMembershipAnswer" runat="server" class="TextBox100" tabindex="6" placeholder="Security Answer" onkeyup="checkanswerlength(); validateForm()" onblur="checkanswerlength(); validateForm()" clientidmode="Static" />
                                <div id="ValidMembershipAnswer" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <p class="textaligncenter"><asp:Label ID="Error" Visible="false" runat="server" CssClass="Error" Text=""></asp:Label></p>
                                <button class="btn btn-primary nextBtn btn-lg pull-right" type="submit" id="btnCreateAccount" disabled="disabled" tabindex="7" clientidmode="Static" onserverclick="SignHimUp" runat="server">Next</button>
                            </div>
                        </div>                    
                        <div class="col-md-3 col-sm-1 col-xs-1"></div>
                    </div>
                    <div class="row setup-content" id="step2" runat="server" clientidmode="Static">
                        <div class="col-md-3 col-sm-1 col-xs-1"></div>
                        <div class="col-md-6 col-sm-10 col-xs-10 textaligncenter">
                            <br />
                            <h3>Personal Details</h3>
                            <div id="AccountAdded" class="alert alert-success alert-dismissible" role="alert" ClientIDMode="Static" runat="server">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <strong>User account created successfully</strong>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtFirstName" runat="server" class="TextBox100" value="" placeholder="Enter First Name" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" tabindex="1" clientidmode="Static" />
                                <div id="ValidtxtFirstName" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered a First Name
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtLastName" runat="server" class="TextBox100" value="" placeholder="Enter Last Name" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" tabindex="2" clientidmode="Static" />
                                <div id="ValidtxtLastName" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered a Last Name
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtEmailAddressConfirm" runat="server" disabled="disabled" required="required" class="TextBox100" clientidmode="Static" placeholder="Enter Email Address" />
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtPhoneNumber" runat="server" class="TextBox100" placeholder="Enter Phone Number" tabindex="3" value="" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" clientidmode="Static" />
                                <div id="ValidtxtPhoneNumber" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered a Phone Number
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtAddressLine1" runat="server" class="TextBox100" placeholder="Enter Address Line 1" tabindex="4" value="" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" clientidmode="Static" />                            
                                <div id="ValidtxtAddressLine1" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered an Address Line 1
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtAddressLine2" runat="server" class="TextBox100" placeholder="Enter Address Line 2" tabindex="5" clientidmode="Static" />
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtCity" runat="server" class="TextBox100" placeholder="Enter City" tabindex="6" value="" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" clientidmode="Static" />
                                <div id="ValidtxtCity" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered a City
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label"></label>
                                <input maxlength="100" type="text" id="txtPostCode" runat="server" class="TextBox100" placeholder="Enter Post Code" value="" onkeyup="checklength(this.value, this.id)" onblur="checklength(this.value, this.id)" tabindex="7" clientidmode="Static" />
                                <div id="ValidtxtPostCode" class="alert alert-danger alert-dismissible textaligncenter" role="alert" ClientIDMode="Static" runat="server" style="display: none">
                                    You have not entered a Post Code
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                </div>
                            </div>
                            <div class="form-group">
                                <input id="chkEmailSubscribe" class="checkbox-custom" runat="server" clientidmode="Static" name="chkEmailSubscribe" type="checkbox" tabindex="8" />
                                <label for="chkEmailSubscribe" class="checkbox-custom-label">If you wish to opt out of receiving emails regarding the latest information and special deals from Bits and Bobs, unchceck the box</label>
                            </div>
                            <button class="btn btn-primary nextBtn btn-lg pull-right" type="button" runat="server" id="btnPersonalDetails" clientidmode="Static" disabled="disabled" onserverclick="PersonalDetailsButton" >Next</button>
                        </div>
                        <div class="col-md-3 col-sm-1 col-xs-1"></div>
                    </div>
                    <div class="row setup-content" id="step3" runat="server" clientidmode="Static">
                        <div class="col-md-12 textaligncenter">
                            <br />
                            <h3>Account Setup</h3>
                            <p>Your account has been successfully created.</p>          
                        </div>
                    </div>
                </form>  
            </div>        
        </ContentTemplate>
        <Triggers>        
            <asp:AsyncPostBackTrigger ControlID="btnCreateAccount" EventName="serverclick" />
            <asp:AsyncPostBackTrigger ControlID="btnPersonalDetails" EventName="serverclick" />
        </Triggers>
    </asp:UpdatePanel>

    Any ideas why this is happening?

    Rob

    Thursday, December 1, 2016 2:19 PM

All replies

  • User283571144 posted

    Hi Robbied81,

    Membership.CreateUser firing twice due to Ajax Toolkit

    According to your description and codes, I try to write a test demo on my computer, but I couldn't reproduce your issue.

    I guess the reason why Membership.CreateUser firing twice is you set a form tag inside the updatepanel.

    I guess when you click the button, you submit firstly then PostBackTrigger fired.

    I suggest you could try to delete the form tag or change the button type.

    Besides, I suggest you could post more relevant codes about the page or could write a little sample project then upload it.

    If you could post more relevant information, it will be more easily for us to reproduce the issue and find the solution .

    Best Regards,

    Brando

    Friday, December 2, 2016 3:31 AM