Problem Statement
Problem 1) Develop an asp.net page that displays a list of options with check boxes (Use CheckBoxList web control). On clicking a button (web control) the page displays the selected options in a label control.
Problem 2) Develop an asp.net page that displays two text boxes and a button web control. The textboxes are used to capture the number of rows and number of columns from user.
Code Implementation
Problem 1: CheckBoxList Control
CheckListTest.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CheckListTest.aspx.vb" Inherits="CheckListTest" %>
<html>
<head runat="server">
<title>CheckBoxTest</title>
</head>
<body>
<form method="post" runat="server">
Choose your favorite programming languages:<br /><br />
<asp:CheckBoxList id="chklst" runat="server" /><br /><br />
<asp:Button id="cmdOK" Text="OK" runat="server" />
<br /><br />
<asp:Label id="lblResult" runat="server" />
</form>
</body>
</html>
CheckListTest.aspx.vb
<script runat="server">
Partial Public Class CheckListTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Me.IsPostBack = False Then
chklst.Items.Add("C")
chklst.Items.Add("C++")
chklst.Items.Add("C#")
chklst.Items.Add("Visual Basic 6.0")
chklst.Items.Add("VB.NET")
chklst.Items.Add("Pascal")
End If
End Sub
Protected Sub cmdOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdOK.Click
lblResult.Text = "You chose:<b>"
Dim lstItem As ListItem
For Each lstItem In chklst.Items
If lstItem.Selected = True Then
' Add text to label.
lblResult.Text &= "<br>" & lstItem.Text
End If
Next
lblResult.Text &= "</b>"
End Sub
End Class
</script>
Problem 2: Table Generation
TableTest.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TableTest.aspx.vb" Inherits="TableTest" %>
<html>
<head runat="server">
<title>Table Test</title>
</head>
<body>
<form method="post" runat="server">
Rows:
<asp:TextBox id="txtRows" runat="server" />
Cols:
<asp:TextBox id="txtCols" runat="server" /><br /><br />
<asp:CheckBox id="chkBorder" runat="server" Text="Put Border Around Cells" />
<br /><br />
<asp:Button id="cmdCreate" runat="server" Text="Create" /><br /><br />
<asp:Table id="tbl" runat="server" />
</form>
</body>
</html>
TableTest.aspx.vb
<script runat="server">
Partial Public Class TableTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Configure the table's appearance.
' This could also be performed in the .aspx file,
' or in the cmdCreate_Click event handler.
tbl.BorderStyle = BorderStyle.Inset
tbl.BorderWidth = Unit.Pixel(1)
End Sub
Protected Sub cmdCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCreate.Click
' Remove all the current rows and cells.
' This would not be necessary if you set EnableViewState = False.
tbl.Controls.Clear()
Dim i, j As Integer
For i = 0 To Val(txtRows.Text - 1)
' Create a new TableRow object.
Dim rowNew As New TableRow()
' Put the TableRow in the Table.
tbl.Controls.Add(rowNew)
For j = 0 To Val(txtCols.Text - 1)
' Create a new TableCell object.
Dim cellNew As New TableCell()
cellNew.Text = "Example Cell (" & i.ToString() & ","
cellNew.Text &= j.ToString() & ")"
If chkBorder.Checked = True Then
cellNew.BorderStyle = BorderStyle.Inset
cellNew.BorderWidth = Unit.Pixel(1)
End If
' Put the TableCell in the TableRow.
rowNew.Controls.Add(cellNew)
Next
Next
End Sub
End Class
</script>