Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Controls within a datalist

 
 
Carl Howarth
Guest
Posts: n/a
 
      07-17-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Mario Vargas
Guest
Posts: n/a
 
      07-17-2003
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>
<aspataList 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>
</aspataList>
</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


 
Reply With Quote
 
 
 
Reply

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



Advertisments