Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Check Box Code to Display the Result of Combination in ASP.NET
WhatsApp
Mithilesh Kumar
May 26
2016
1.2
k
0
1
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
Up Next
Check Box Code to Display the Result of Combination in ASP.NET