Reputation: 47
I have two button control. I want to disable the field validator for textboxes when I click btnavail_Click
. Its working. But when I click the button btnsubmit_Click
the field validator is not enabled.
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 causes validation property for both the buttons to false. I need to enable the validator when I click btnsubmit_Click
. Please someone 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 my HTML code. I want to disable Field validator for studentid,no of books and password on btnavail_click. Again I need to enable them for btnsubmit_click
Upvotes: 3
Views: 5621
Reputation: 21795
That's because, you are enabling your RequiredFieldValidator
in server side code i.e. on button click event handler and validation controls like RequiredFieldValidator
works on client side. This means when you click on Submit
buttton, since your validator was disabled it didn't worked and form was posted to the server, now even if you enable the Validator now, its of no use since form is already posted.
Instead of Enabling & Disabling the validator controls, you should set the CausesValidation property for each button control to 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 btnavail
button, set CausesValidation="false"
for this button, your mark-up should look like:-
<asp:Button ID="btnavail" runat="server" Text="Book Availability"
CausesValidation="false" OnClick="btnavail_Click" />
Upvotes: 0
Reputation: 4354
Why not add ValidationGroup
to your textbox and button(btnsubmit) something like this
<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 common validation group to textboxes(on which i wanted to fire a required field validation) and submit button. Hence all my validations will fire only on submit button.
The benefit of using this approach is all your work will be done client side and no need to take an extra headache of enabling and disabling the validations.
Update Just use this code as it 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>
Upvotes: 1