hi all
i have one form in which i have multiple checkbox options for the user to select each checkbox. i want to validate each chekbox when click on the Button control. if any checkbox is not checked it will show error for a particular checkbox that is not checked.
Loading


Ankit NandekarRank #703 · 3.4K reputationPosted Apr 27, 2011, 10:25 PM EDT
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
int c = 0;
protected void Page_Load(object sender, EventArgs e)
{
lblMsg.Visible = false;
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control chkcontrol in form1.Controls)
{
if (chkcontrol.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
{
if (((CheckBox)chkcontrol).Checked == true)
{
c++;
// write code when check box checked
}
else
{
if (c == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "please select CheckBox";
}
}
}
}
}
}
omkar mhaiskar84 reputationPosted Apr 27, 2011, 10:30 PM EDT
you can check it through Custom validator and use validation summary to show ewrror message.
I think this will help:
function ValidateAnswerRadio()
{
var list = document.getElementById('<%=RadioButtons.ClientID %>').childNodes;
for(i = 0; i < list.length; i++)
if(list[i].type == "radio")
if(list[i].checked) return true;
return false;
}
Required.
And for validation summary use following code.Ankit NandekarRank #703 · 3.4K reputationPosted Apr 27, 2011, 11:36 PM EDT
Rohatash KumarRank #12 · 57.7K reputationPosted Apr 28, 2011, 1:23 AM EDT