0% found this document useful (0 votes)
24 views2 pages

Check Box

The document provides code samples for assigning text to Checkbox controls in ASP.NET and determining which checkbox is selected when a button is clicked. It also shows how to validate a checkbox control to require it is checked using both client and server side validation. Code snippets in both VB.NET and C# are given.

Uploaded by

api-3841500
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Check Box

The document provides code samples for assigning text to Checkbox controls in ASP.NET and determining which checkbox is selected when a button is clicked. It also shows how to validate a checkbox control to require it is checked using both client and server side validation. Code snippets in both VB.NET and C# are given.

Uploaded by

api-3841500
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

CheckBox:

10.1 How to assign a text to Checkbox control and know which checkbox
is selected?

<asp:CheckBox id="chkTips" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 64px"
runat="server"
Text="Tips"></asp:CheckBox>
<asp:CheckBox id="chkTricks" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 112px"
runat="server" Text="Tricks"></asp:CheckBox>
<asp:CheckBox id="chkFaqs" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 160px"
runat="server"
Text="Faqs"></asp:CheckBox>
<asp:Button id="btnSelect" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 224px"
runat="server" Text="Select"></asp:Button>

VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


'Put user code to initialize the page here
If Not Page.IsPostBack Then
End If
End Sub

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnSelect.Click
Dim ctl As Control
Dim strchkSelected As String
For Each ctl In Page.Controls(1).Controls
If TypeOf ctl Is CheckBox Then
if CType(ctl, CheckBox).Checked Then
strchkSelected += CType(ctl, CheckBox).Text + " "
End If
End If
Next
Response.Write(strchkSelected)
End Sub

C#

private void Page_Load(object sender, System.EventArgs e)


{
// Put user code to initialize the page here
if (!Page.IsPostBack )
{
}
}

private void btnSelect_Click(object sender, System.EventArgs e)


{
string strchkSelected="" ;
foreach (Control ctl in Page.Controls[1].Controls )
{
CheckBox chk = ctl as CheckBox ;
if (chk!=null)
{
if (chk.Checked )
{
strchkSelected += chk.Text + " " ;
}
}
}
Response.Write (strchkSelected);
}

10.2 How to validate a CheckBox?

<asp:CheckBox runat="server" id="chkbox"></asp:CheckBox>


<asp:CustomValidator runat="server" ErrorMessage="Error!" OnServerValidate="ServerValidation"
ClientValidationFunction="ClientValidation"
ID="Customvalidator1" />
<asp:Button runat="server" text="submit" ID="Button1" />

<script language="javascript">
function ClientValidation(source, args)
{
args.IsValid = document.all["chkbox"].checked;
}
</script>

VB.NET

<script runat="server" language="vb">


sub ServerValidation( source as object, args as ServerValidateEventArgs )
args.IsValid = chkbox.Checked
end sub
</script>

C#

<script runat="server" language="cs">


void ServerValidation(object source, ServerValidateEventArgs args)
{
args.IsValid = chkbox.Checked;
}
</script>

You might also like