how to add required validation in radio button
<td class="auto-style8"> </td>
<td class="auto-style15">First Noon</td>
<td colspan="2">
<asp:RadioButton ID="RadioButton1" runat="server" CssClass="auto-style27" GroupName="fn" Text="YES" />
|
<asp:RadioButton ID="RadioButton2" runat="server" CssClass="auto-style27" GroupName="fn" Text="NO" />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator4"
ControlToValidate="RadioButton2" Text="Required"
ValidationGroup="fn">
</asp:RequiredFieldValidator>
</td>
protected void Button1_Click(object sender, EventArgs e)
{
// string Id = TextBox1.Text.Trim();
string doc_nm = TextBox2.Text.Trim();
string spl = TextBox3.Text.Trim();
string room = TextBox8.Text.Trim();
string fn = "";
string an = "";
string rmk = TextBox7.Text.Trim();
string sts = string.Empty;
if (RadioButton1.Checked)
{
fn = "YES";
}
if (RadioButton2.Checked)
{
fn = "NO";
}
if (RadioButton3.Checked)
{
an = "YES";
}
if (RadioButton4.Checked)
{
an = "NO";
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO spslst(doc_nm,spl,room,fn,an,rmk) VALUES(@doc_nm,@spl,@room,@fn,@an,@rmk)"))
{
cmd.Connection = con;
//cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@doc_nm", doc_nm);
cmd.Parameters.AddWithValue("@spl", spl);
cmd.Parameters.AddWithValue("@room", room);
cmd.Parameters.AddWithValue("@fn", fn);
cmd.Parameters.AddWithValue("@an", fn);
cmd.Parameters.AddWithValue("@rmk", rmk);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
string message = "DOCTOR's details have been saved successfully.";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += Request.Url.AbsoluteUri;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
}
Error.:-
Server Error in '/' Application.
Control 'RadioButton2' referenced by the ControlToValidate property of 'RequiredFieldValidator4' cannot be validated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: Control 'RadioButton2' referenced by the ControlToValidate property of 'RequiredFieldValidator4' cannot be validated. Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
Stack Trace:
[HttpException (0x80004005): Control 'RadioButton2' referenced by the ControlToValidate property of 'RequiredFieldValidator4' cannot be validated.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +9855039
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +42
System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +38
System.Web.UI.Control.PreRenderRecursiveInternal() +90
System.Web.UI.Control.PreRenderRecursiveInternal() +163
System.Web.UI.Control.PreRenderRecursiveInternal() +163
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +906
|