Enabling or disabling field validation for a button click

I have two control buttons. I want to disable the margin checker for text boxes when I click btnavail_Click

. Works. But when I click the button btnsubmit_Click

, the field validator is not activated.

 protected void btnsubmit_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator2.Enabled = true;
            RequiredFieldValidator3.Enabled = true;
            RequiredFieldValidator4.Enabled = true;
            con.Open();
            SqlCommand cmd1 = new SqlCommand("Select * from tblstudentinfo where StudentId=@studentid", con);
            cmd1.Parameters.AddWithValue("@studentid",txtstudentid.Text.ToString());
            SqlDataReader rdr=cmd1.ExecuteReader();
            if(rdr.Read())
            {
                int i=Convert.ToInt32(rdr["Password"]);
                int j=Convert.ToInt32(txtpassword.Text);
                if(i == j)
                {
                    SqlCommand cmd2 = new SqlCommand("select * from tblbookinfo where Name=@bookname", con);
                    cmd2.Parameters.AddWithValue("@bookname", txtbookname.Text.ToString());
                    SqlDataReader rdr1 = cmd2.ExecuteReader();
                    if (rdr1.Read())
                    {
                        int s = Convert.ToInt32(rdr1["BookId"]);
                        SqlCommand cmd = new SqlCommand("inserttotbllendinginfo2", con);
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@bookid",
                            SqlDbType = SqlDbType.Int,
                            Value = s
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@studentid",
                            SqlDbType = SqlDbType.Int,
                            Value = Convert.ToInt32(txtstudentid.Text)
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@noofbooks",
                            SqlDbType = SqlDbType.Int,
                            Value = Convert.ToInt32(txtnoofbook.Text)
                        });
                        cmd.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@dateoflending",
                            SqlDbType = SqlDbType.Date,
                            Value = txtdate.Text
                        });
                        cmd.ExecuteNonQuery();
                    }
                 }
                else
                {
                    lblpassword.Visible=true;
                    lblpassword.Text="Your password is incorrect. Please Check it";
                    lblpassword.ForeColor = System.Drawing.Color.Red;
                }

                con.Close();
              }
        }

        protected void btnavail_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator2.Enabled = false;
            RequiredFieldValidator3.Enabled = false;
            RequiredFieldValidator4.Enabled = false;

            SqlCommand cmd = new SqlCommand("select * from tblbookinfo where Name=@bookname", con);
            cmd.Parameters.AddWithValue("@bookname", txtbookname.Text.ToString());
            con.Open();
            if (String.IsNullOrEmpty(txtbookname.Text))
            {
                lblbookavail.Visible = true;
                lblbookavail.Text = "Please enter a valid book name";
                lblbookavail.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
            SqlDataReader rdr = cmd.ExecuteReader();            
                if (rdr.Read())
                {
                    string s = rdr["Name"].ToString();
                    if (s != null)
                    {
                        int i = Convert.ToInt32(rdr["AvailableBooks"]);
                        lblbookavail.Visible = true;
                        lblbookavail.Text = (i.ToString());
                    }
            }
            }
           con.Close();
            }

      

I made the validation property for both buttons to false. I need to enable the validator when I click btnsubmit_Click

. Please help me.

<table align="center">
            <tr><td>Book Name</td><td><asp:TextBox ID="txtbookname" runat="server"></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Requires book name" ControlToValidate="txtbookname" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td>
                <td><asp:Button ID="btnavail" runat="server" Text="Book Availability"   OnClick="btnavail_Click" /></td><td><asp:Label ID="lblbookavail" runat="server" Visible="false"></asp:Label></td></tr>
            <tr><td>Student Id</td><td><asp:TextBox ID="txtstudentid" runat="server" ValidationGroup="save"></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Requires Studentid" ControlToValidate="txtstudentid" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td></tr>
            <tr><td>No of Book</td><td><asp:TextBox ID="txtnoofbook" runat="server" ></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Requires no of book" ControlToValidate="txtnoofbook" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td></tr>
            <tr><td>Date of lending</td><td><asp:TextBox ID="txtdate" runat="server" ></asp:TextBox></td></tr>
            <tr><td>Password</td><td><asp:TextBox ID="txtpassword" TextMode="Password" runat="server"  ></asp:TextBox></td>
                <td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Requires Studentid" ControlToValidate="txtpassword" ForeColor="Red" Text="*"></asp:RequiredFieldValidator></td>
                <td><asp:Label ID="lblpassword" runat="server" Visible="false"></asp:Label></td></tr>
            <tr><td><asp:Button ID="btnsubmit" runat="server" Text="Submit"  OnClick="btnsubmit_Click" /></td></tr>
            </table>

      

This is my HTML code. I want to disable field validator for student, no books and no password on btnavail_click. Again I need to enable them for btnsubmit_click

+3


source to share


2 answers


This is because you included yours RequiredFieldValidator

in the server code, that is, on the button click event handler and validation elements such as RequiredFieldValidator

work on the client side. This means that when you click the Submit

buttton, since your validator was disabled, it didn't work and the form was submitted to the server, now even if you enabled the Validator function, it doesn't make sense since the form has already been submitted.

Instead of enabling and disabling validator check items, you should set the CausesValidation property for each button control with either true

, or false

, like this: -

<asp:Button ID="SubmitButton" runat="server" Text="Submit" 
            CausesValidation="true" OnClick="SubmitButton_Click" />

      



Update:

Since you don't want validation on a button btnavail

, set for that button CausesValidation="false"

, your markup should look like this: -

<asp:Button ID="btnavail" runat="server" Text="Book Availability" 
            CausesValidation="false" OnClick="btnavail_Click" />

      

0


source


Why not add ValidationGroup

something like this to the textbox and button (btnsubmit)

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="save"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" 
            ValidationGroup="save"></asp:RequiredFieldValidator>
        <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="save"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" 
            ValidationGroup="save"></asp:RequiredFieldValidator>
        <asp:Button ID="btnAvail" runat="server" Text="Button" ValidationGroup="save" 
            onclick="btnAvail_Click" />
        <asp:Button ID="btnsubmit" runat="server" Text="Button" 
            onclick="btnsubmit_Click" />

      

Here, I gave a general validation group to text fields (on which I wanted to run a mandatory field validation) and a submit button. Hence, all my checks will only fire when the button is submitted.



The advantage of using this approach is that all your work will be done on the client side and you don't have to take the extra headache to enable and disable validation.

Update Just use this code as is.

<table align="center">
    <tr>
        <td>
            Book Name
        </td>
        <td>
            <asp:TextBox ID="txtbookname" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Requires book name"
                ControlToValidate="txtbookname" ForeColor="Red" Text="*"  ValidationGroup="avail"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Book Availability" OnClick="btnavail_Click"  ValidationGroup="avail"/>
        </td>
        <td>
            <asp:Label ID="lblbookavail" runat="server" Visible="false"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            Student Id
        </td>
        <td>
            <asp:TextBox ID="txtstudentid" runat="server" ValidationGroup="save"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Requires Studentid"
                ControlToValidate="txtstudentid" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            No of Book
        </td>
        <td>
            <asp:TextBox ID="txtnoofbook" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Requires no of book"
                ControlToValidate="txtnoofbook" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Date of lending
        </td>
        <td>
            <asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Password
        </td>
        <td>
            <asp:TextBox ID="txtpassword" TextMode="Password" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Requires Studentid"
                ControlToValidate="txtpassword" ForeColor="Red" Text="*"  ValidationGroup="save"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Label ID="lblpassword" runat="server" Visible="false"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button2" runat="server" Text="Submit" 
                OnClick="btnsubmit_Click"  ValidationGroup="save" style="height: 26px"/>
        </td>
    </tr>
</table>

      

0


source







All Articles