Controls within a datalist

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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
    [email]carl.howarth@m-s-solutions.co.uk[/email]


    Carl Howarth Guest

  2. Similar Questions and Discussions

    1. add server controls to the templates for the DataList control.
      how do I load controls to the templates for the DataList control. <asp:datalist id="dl1" runat="server" > <ItemTemplate> " how to load...
    2. Adding recursive datalist controls
      Hello, I created a webservercontrol extended from the datalist coontrol. This control allows me to add one nested datalist, which is inside a...
    3. How to access the controls that are inside of a DataList/DataGrid using JS?
      My problem is simple, i have a datalist control with some controls inside of EditTemplate (the problem is the same in the DataGrid). I want to have...
    4. Accessing controls within a datalist footer template
      Hi, I am a bit green when it comes to .net development so please excuse me if this question is obvious or has been asked before... I am...
    5. ANNOUNCEMENT : Samples for Datagrid, Datalist, Data Repeater controls
      hi all, Check this link for updated scrollable grid sample and download gridtest.zip from my site for the source....
  3. #2

    Default Re: Controls within a datalist

    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


    Mario Vargas Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139