TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Check Box Code to Display the Result of Combination in ASP.NET
Mithilesh Kumar
May 26
2016
Code
1.1
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Default.aspx
<
html
>
<
head
runat
=
"server"
>
<
title
>
</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
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
=
"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>"
;
}
}
}
Check Box Code
ASP.NET