Default.aspx
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div align="center" >
<div style="width:500px">
<h1 style="border:2px solid
green">Welcome to Check Box Example</h1>
<asp:Panel ID="panel" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" Text="Option1" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="Option2" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="Option3" />
<asp:CheckBox ID="CheckBox4" runat="server" Text="Option4" />
<asp:CheckBox ID="CheckBox5" runat="server" Text="Option5" />
<br /><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br /><br />
<asp:Label ID="lblStatus" Style="font-family:cursive" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</div>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblStatus.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
lblStatus.Visible = true;
string strSelectedOption = string.Empty;
foreach (Control chkitem in panel.Controls)
{
if (chkitem.GetType().Name == "CheckBox")
{
CheckBox chk = (CheckBox)(chkitem);
if (chk.Checked == true)
{
strSelectedOption +=
chk.Text + ",
";
}
}
}
if (strSelectedOption !=
"")
{
strSelectedOption =
strSelectedOption.Remove(strSelectedOption.Length - 1);
int pos = strSelectedOption.LastIndexOf(", ");
int x = 0;
if (pos != -1)
{
strSelectedOption =
strSelectedOption.Remove(pos, 1);
strSelectedOption =
strSelectedOption.Insert(pos, " and ");
}
string str1 =
strSelectedOption.Remove(strSelectedOption.LastIndexOf(","));
string s = str1 + str1.Replace(str1, ".");
lblStatus.Text = "<b>Selected
Check box : </b>" + s + " ";
}
else
{
lblStatus.Text = "<b
style='color:red'>Please select at least one check box</b>";
}
}
}
Output:
No comments:
Post a Comment