468,241 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

Controls within a datalist

Hi,

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

Many thanks, Carl Howarth
ca**********@m-s-solutions.co.uk
Nov 17 '05 #1
1 5551
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>&nbsp;</p>
<asp:DataList 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_SelectedInd exChanged"></asp:CheckBoxL
ist>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
</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_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
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).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,
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
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paul Maidment | last post: by
1 post views Thread by Scott Schluer | last post: by
1 post views Thread by bill yeager | last post: by
reply views Thread by bill yeager | last post: by
8 posts views Thread by MattB | last post: by
4 posts views Thread by V. Jenks | last post: by
reply views Thread by manu_srinivasa | last post: by
1 post views Thread by WB | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.