2

I am working on a eCommerce project which has an admin panel and shopping panels.

I have finished programming and now testing every single aspx and cs files by manually.

The problem is, I have a change password feature which is related with session and Database. The problem is I have a validators in my aspx file but they won't work. Here my codes are;

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Panel ID="Panel1" runat="server" DefaultButton="btnChange">
        <div class="userForm">
            <div class="formTitle">
                Change Your Password
            </div>
            <div class="formContent">
                <asp:TextBox ID="txtPassword" placeholder="Type your new password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtPassword"
                    ErrorMessage="RequiredFieldValidator" ForeColor="Red" Display="Dynamic" ValidationGroup="signup">Enter a password</asp:RequiredFieldValidator>
                <br />
                <asp:TextBox ID="txtAgainPassword" placeholder="Repeat your new password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" BorderColor="Red"
                    ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Enter password again."
                    ForeColor="Red" ValidationGroup="signup"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
                    ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Password don't match."
                    ForeColor="Red" ValidationGroup="signup"></asp:CompareValidator>
                <br />
                <asp:Button ID="btnChange" runat="server" Text="Submit" OnClick="btnChange_Click" />
                <br />
                <asp:Label ID="lblError" Visible="False" ForeColor="Green" runat="server"></asp:Label></div>
        </div>
    </asp:Panel>
</asp:Content>

and the .cs part is below

protected void btnChange_Click(object sender, EventArgs e)
{
    using (ZirveMarketDBEntities context = new ZirveMarketDBEntities())
    {
        // Atanan sessiona gore user bilgisini al - guvenlik icin onemli
        int id = (int)Session["CustomerID"];
        Customer cust = context.Customers.Where(i => i.CustomerID == id).FirstOrDefault();

        cust.Password = txtPassword.Text;
        context.SaveChanges();
    }
    lblError.Visible = true;
    lblError.Text = "Password successfully updated";
}

The problem is, I have a 2 box for new password and type new password. Even if they are null, even if they don't match the password still changes with the value of the first part. I don't want to run server side code if they don't match or null. What am I doing wrong? Helps are pretty apreciated.

0

2 Answers 2

2

Add the 'ValidationGroup="signup"' attribute to the btnChange button.

I'd also recommend adding the below to the click event (before anything else) in case Javascript is disabled on the client:

Page.Validate("signup");

if (!Page.IsValid)
{
    return;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, How could I miss that! Seems its time to take a break for me :) Thanks for the answer, I will also consider your recommend on each buttons I use, thanks!
Thanks for this. For some reason when using placeholders w/ asp TextBox controls, the required validator would not work in IE9 and 8. Adding this code to btn submit event fixed it.
1

You have validation groups specified on the validators, but not on the Button. Try adding the validation group to the button as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.