Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Controls within a datalist

Thread Tools

Controls within a datalist

Carl Howarth
Posts: n/a

I have a check box list within a datalist. Can anybody tell me how I go
about accessing the onclick event of these checkbox lists? (using

Many thanks, Carl Howarth Removed)

Reply With Quote
Mario Vargas
Posts: n/a

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.


<!----------- WebForm1.aspx --------------->
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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"
<form id="Form1" method="post" runat="server">
<b>Selected Titles: </b>
<asp:Label ID="selectedTitles" Runat="server" />
<asp:Button ID="myBtn" Runat="server" Text="Submit" />
<aspataList ID="myDataList" Runat="server">
<asp:Label ID="authorId" Text='<%# Container.DataItem( "au_id" ) %>'
<asp:Label ID="authorName" Text='<%# Container.DataItem( "au_lname" ) +
", " + Container.DataItem( "au_fname" ) %>' Runat="server">
<asp:CheckBoxList ID="myCheckBoxList" Runat="server"
OnSelectedIndexChanged="myCheckBoxList_SelectedInd exChanged"></asp:CheckBoxL
<hr />

<!------------- 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_conne ction=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

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.
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)

Dim cmd As New SqlCommand("SELECT * FROM authors", conn)

myDataList.DataSource =
Catch ex As Exception
' TODO: Log error
If ConnectionState.Open = conn.State Then
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"),
' Get the list of titles
Dim myCheckBoxList As CheckBoxList =
CType(e.Item.FindControl("myCheckBoxList"), CheckBoxList)

Dim conn As New SqlConnection(ConnectionString)
' 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)"
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 =
Catch ex As Exception
' TODO: Log error
If ConnectionState.Open = conn.State Then
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
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).App end(", ")
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,

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

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with DataList within DataList Will ASP .Net Web Controls 4 10-18-2007 03:19 AM
Setting up a datalist control - Item_DataBound for a datalist in a datalist Nevyn Twyll ASP .Net 8 09-09-2004 10:13 PM
How to handle Dynamic Controls within User Controls Michael Ramey ASP .Net 2 02-04-2004 06:46 PM
Accessing controls within a datalist footer template Paul Maidment ASP .Net 1 08-07-2003 04:39 PM
Controls within a datalist Carl Howarth ASP .Net Web Controls 1 07-18-2003 03:58 PM