CheckBoxList values not appearing and State not maintained

Discussion in 'ASP .Net Web Controls' started by jmhmaine, Jan 20, 2005.

  1. jmhmaine

    jmhmaine Guest

    I have a CheckBoxList control nested in a DataList <selectedItemTemplate>.
    The data source is from SQL Server tables, so the boxes are built
    dynamically. Here is the ASPX page:

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="simpleData.aspx.vb" Inherits="simpleData" Trace="True" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>simpleData</title>
    </head>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:DataList ID="dlEmail_Parent"
    Runat="server"
    RepeatLayout="Flow"
    DataKeyField="FamilyID"
    OnItemCommand="DoItemSelect"
    OnCancelCommand="DoItemCancel"
    EnableViewState="True">
    <ItemTemplate>
    <asp:ImageButton CommandName="Select"
    ImageUrl='/images/page_elements/uparrow.gif' Height="10" Width="11"
    runat="server" ID="Imagebutton1" EnableViewState=False />
    <%# DataBinder.Eval(Container.DataItem, "FamilyName") %><br>
    </ItemTemplate>
    <SelectedItemTemplate>
    <asp:ImageButton CommandName="Cancel"
    ImageUrl='/images/page_elements/downarrow.gif' Height="10" Width="11"
    runat="server" ID="Imagebutton2" EnableViewState=False />
    <%# DataBinder.Eval(Container.DataItem, "FamilyName") %><br>
    <asp:CheckBoxList id="chblEmailSignup"
    DataSource='<%# GetChildRelation(Container.DataItem,
    "PROJECT_FAMILY_PROJECT") %>'
    DataMember='PROJECT'
    DataTextField='ProjectName'
    DataValueField='ProjectID'
    EnableViewState="True"
    OnSelectedIndexChanged="Check_Clicked"
    Runat="server">
    </asp:CheckBoxList>
    <asp:Button ID="submitSelections" Text="Submit Selections" Runat="server"
    EnableViewState=False />
    </SelectedItemTemplate>
    </asp:DataList>
    <br />
    <b><asp:Literal ID="ltrEmailSignup" Runat="server"
    EnableViewState=False /></b>
    </form>

    </body>
    </html>

    I am able get the values to appear correctly, but I am not able to retireve
    which boxes are checked. Also, I am not able to keep the state of the boxes,
    so
    if the user clicks a box, goes to another item and then returns, the box is
    not checked.

    Here is the VB Code Behind:

    Imports System.Data.SqlClient

    Public Class simpleData
    Inherits System.Web.UI.Page

    Protected WithEvents dlEmail_Parent As DataList
    Protected WithEvents ltrEmailSignup As Literal

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    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
    If Not (IsPostBack) Then
    bindData()
    End If

    End Sub


    Sub Check_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
    'ltrEmailSignup.Text = "Selected Item(s):<br><br>" &
    chblEmailSignup.ClientID

    ' Iterate through the Items collection of the CheckBoxList
    ' control and display the selected items.
    Dim selectedCBL As CheckBoxList =
    CType(Me.FindControl("chblEmailSignup"), CheckBoxList)

    Dim i As Integer

    If (Not selectedCBL Is Nothing) Then
    For i = 0 To selectedCBL.Items.Count - 1
    If selectedCBL.Items(i).Selected Then
    ltrEmailSignup.Text &= selectedCBL.Items(i).Text &
    "<br>"
    End If
    Next
    Else
    ltrEmailSignup.Text = "control is equal to nothing" ' I AM
    STUCK HERE!!!!
    End If


    End Sub


    Sub bindData()
    Dim sqlParent As String = "SELECT FamilyID,FamilyName FROM
    PROJECT_FAMILY;"
    Dim sqlChild As String = "SELECT ProjectID, ProjectName,
    FamilyID FROM PROJECT;"
    Dim ds As New DataSet
    Dim conn As New SqlConnection

    conn.ConnectionString = Global.ConnectionString

    'Two commands for two tables
    Dim cmdParent As SqlCommand
    Dim cmdChild As SqlCommand

    'Two datapaters to fill the dataset from two tables
    Dim adpParent As SqlDataAdapter
    Dim adpChild As SqlDataAdapter


    'This handles the relationship between the two columns
    Dim datarelation As datarelation
    Dim dcParent As DataColumn
    Dim dcChild As DataColumn

    'First command for first table
    cmdParent = New SqlCommand
    cmdParent.Connection = conn
    cmdParent.CommandText = sqlParent

    'Second command for Second table
    cmdChild = New SqlCommand
    cmdChild.Connection = conn
    cmdChild.CommandText = sqlChild

    'Now , we will fill the first table and add it to the dataset
    adpParent = New SqlDataAdapter
    adpParent.SelectCommand = cmdParent
    adpParent.TableMappings.Add("Table", "PROJECT_FAMILY")
    adpParent.Fill(ds)


    'As we did in the previous step , here for the Second table
    adpChild = New SqlDataAdapter
    adpChild.SelectCommand = cmdChild
    adpChild.TableMappings.Add("Table", "PROJECT")
    adpChild.Fill(ds)

    dcParent = ds.Tables("PROJECT_FAMILY").Columns("FamilyID")
    dcChild = ds.Tables("PROJECT").Columns("FamilyID")

    'Here we combined two datacolumns to the relations obj
    datarelation = New datarelation("PROJECT_FAMILY_PROJECT",
    dcParent, dcChild)
    ds.Relations.Add(datarelation)

    dlEmail_Parent.DataSource = ds.Tables("PROJECT_FAMILY")
    dlEmail_Parent.DataBind()


    adpParent.Dispose()
    adpParent = Nothing
    adpChild.Dispose()
    adpChild = Nothing
    ds.Dispose()
    ds = Nothing
    conn.Close()
    conn = Nothing
    End Sub

    Function GetChildRelation(ByVal dataItem As Object, ByVal relation
    As String) As DataView
    'TODO: Test for Null DataItem
    Dim drv As DataRowView = CType(dataItem, DataRowView)
    Return drv.CreateChildView(relation)
    End Function


    '====================================================================================
    'Sub DoItemSelect(ByVal objSource As Object, ByVal objArgs As
    DataListCommandEventArgs)
    '
    'Handles the event of the Item Select Command from web page

    '====================================================================================
    Sub DoItemSelect(ByVal objSource As Object, ByVal objArgs As
    DataListCommandEventArgs)

    dlEmail_Parent.SelectedIndex = objArgs.Item.ItemIndex 'Set the
    Seleted Item

    bindData()
    End Sub


    '====================================================================================
    'Sub DoItemCancel(ByVal objSource As Object, ByVal objArgs As
    DataListCommandEventArgs)
    '
    'Handles the event of the Item Cancel Command from web page

    '====================================================================================
    Sub DoItemCancel(ByVal objSource As Object, ByVal objArgs As
    DataListCommandEventArgs)
    dlEmail_Parent.SelectedIndex = -1 'Leave Select Mode
    bindData()
    End Sub

    End Class
     
    jmhmaine, Jan 20, 2005
    #1
    1. Advertising

  2. jmhmaine

    jmhmaine Guest

    I have partical success, I found a great article on FindControl:
    http://www.odetocode.com/Articles/116.aspx

    So I changed my orginal code from
    Dim selectedCBL As CheckBoxList = CType(Me.FindControl("chblEmailSignup"),
    CheckBoxList)

    To:

    Dim selectedCBL As CheckBoxList =
    CType(dlEmail_Parent.Items(dlEmail_Parent.SelectedIndex).FindControl("chblEmailSignup"), CheckBoxList)

    I'm still having a problem keeping the checkboxes checked between submits. I
    believe this is also affecting my ASP:Literal control because the value when
    another item is selected.

    --jmhmaine
     
    jmhmaine, Jan 20, 2005
    #2
    1. Advertising

  3. Hi Jmhmaine,

    Thanks for your posting. Regarding on this issue I've also found your
    another issue
    titled:
    CheckBoxList not maintaining Checked CheckBoxes when Nested

    in this newsgroup, I've posted my reply there. I'd appreciate if you have a
    look there. In addition, if you feel convenient that we continue to discuss
    in that thread, please feel free to post there. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 21, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JD
    Replies:
    5
    Views:
    11,844
    NetArchitect
    Aug 8, 2007
  2. =?Utf-8?B?SXVsaWFu?=

    Session State not maintained asp.net 2.0

    =?Utf-8?B?SXVsaWFu?=, Mar 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    2,107
    =?Utf-8?B?SXVsaWFu?=
    Mar 28, 2006
  3. chris
    Replies:
    0
    Views:
    343
    chris
    Oct 13, 2006
  4. Santel

    Selected state not maintained on postback

    Santel, Jun 12, 2007, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    927
    Teemu Keiski
    Jun 13, 2007
  5. jmhmaine
    Replies:
    0
    Views:
    310
    jmhmaine
    Jan 20, 2005
Loading...

Share This Page