Carl,
Here's an example. I hope it helps. I included the HTML as well as the
code-behind. Change the ConnectionString constant in WebForm1.aspx.vb to
suit your database server's configuration. Note that you need your SQL
Server database must contain the "pubs" database.
I simply created a Function that loops through each item in the DataList.
For each item, I get a reference of the CheckBoxList and append the selected
item's value as a comma-delimited list to a StringBuilder. When the loops
end, I return the StringBuilder as a String.
You can also use the CheckBoxList's SelectedIndexChanged event to perform
operations on the selected items of the CheckBoxList as well.
I hope that helps.
Mario
<!----------- WebForm1.aspx --------------->
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="CheckBoxListWithinDataList.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<b>Selected Titles: </b>
<asp:Label ID="selectedTitles" Runat="server" />
<br>
<asp:Button ID="myBtn" Runat="server" Text="Submit" />
<p> </p>
<asp
ataList ID="myDataList" Runat="server">
<ItemTemplate>
<asp:Label ID="authorId" Text='<%# Container.DataItem( "au_id" ) %>'
Runat="server">
</asp:Label>
-
<asp:Label ID="authorName" Text='<%# Container.DataItem( "au_lname" ) +
", " + Container.DataItem( "au_fname" ) %>' Runat="server">
</asp:Label>
<asp:CheckBoxList ID="myCheckBoxList" Runat="server"
OnSelectedIndexChanged="myCheckBoxList_SelectedIndexChanged"></asp:CheckBoxL
ist>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp
ataList>
</form>
</body>
</HTML>
<!------------- WebForm1.aspx.vb ------------->
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class WebForm1
Inherits System.Web.UI.Page
Private Const ConnectionString As String =
"server=(local)\netsdk;database=pubs;trusted_connection=yes;"
Protected WithEvents myLabel As System.Web.UI.WebControls.Label
Protected WithEvents myBtn As System.Web.UI.WebControls.Button
Protected WithEvents myDataList As System.Web.UI.WebControls.DataList
Protected WithEvents selectedTitles As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
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
' Show all authors in the database
Dim conn As New SqlConnection(ConnectionString)
Try
conn.Open()
Dim cmd As New SqlCommand("SELECT * FROM authors", conn)
myDataList.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
myDataList.DataBind()
Catch ex As Exception
' TODO: Log error
Finally
If ConnectionState.Open = conn.State Then
conn.Close()
End If
End Try
End If
End Sub
Protected Sub myDataList_ItemDataBound(ByVal sender As System.Object,
ByVal e As DataListItemEventArgs) Handles myDataList.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType
= ListItemType.Item Then
' Populate the CheckBoxList for the current author with the
author's published titles
' Get the author's ID
Dim authorId As Label = CType(e.Item.FindControl("authorId"),
Label)
' Get the list of titles
Dim myCheckBoxList As CheckBoxList =
CType(e.Item.FindControl("myCheckBoxList"), CheckBoxList)
Dim conn As New SqlConnection(ConnectionString)
Try
' Get all titles for the current author from the database
Dim titleAuthorsQuery As String = "SELECT titles.title_id,
titles.title, titles.price, titles.pubdate FROM titles INNER JOIN
titleauthor ON titles.title_id = titleauthor.title_id WHERE (
titleauthor.au_id = @AuthorID)"
conn.Open()
Dim cmd As New SqlCommand(titleAuthorsQuery, conn)
cmd.Parameters.Add("@AuthorID", authorId.Text)
' DataBind the list of titles
myCheckBoxList.DataTextField = "title"
myCheckBoxList.DataValueField = "title_id"
myCheckBoxList.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
myCheckBoxList.DataBind()
Catch ex As Exception
' TODO: Log error
Finally
If ConnectionState.Open = conn.State Then
conn.Close()
End If
End Try
End If
End Sub
Protected Sub myCheckBoxList_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
' TODO: Write code to manipulate the sender (a reference to a
CheckBoxList) when an item is selected
End Sub
' Collects all selected titles and returns them as a comma-separated
list
Private Function CollectSelectedTitles() As String
Dim titlesSb As New StringBuilder()
Dim i As Integer
' Go through each DataListItem in myDataList
For i = 0 To myDataList.Items.Count - 1
Dim curItem As DataListItem = myDataList.Items(i)
If curItem.ItemType = ListItemType.AlternatingItem Or
curItem.ItemType = ListItemType.Item Then
' Get the CheckBoxList in the curItem DataListItem
Dim myCheckBoxList As CheckBoxList =
CType(curItem.FindControl("myCheckBoxList"), CheckBoxList)
' Go through each CheckBoxList item.
' If a CheckBoxList item is selected, append it to the
titlesSb StringBuilder
Dim j As Integer
For j = 0 To myCheckBoxList.Items.Count - 1
If myCheckBoxList.Items(j).Selected Then
titlesSb.Append(myCheckBoxList.Items(j).Value).Append(", ")
End If
Next j
End If
Next i
' Remove the last ", " in the list of titles
Dim commaSpace As String = ", "
titlesSb.Remove(titlesSb.Length - commaSpace.Length,
commaSpace.Length)
CollectSelectedTitles = titlesSb.ToString()
End Function
Private Sub myBtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myBtn.Click
selectedTitles.Text = CollectSelectedTitles()
End Sub
End Class