Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > CheckBoxList values not appearing and State not maintained

Reply
Thread Tools

CheckBoxList values not appearing and State not maintained

 
 
jmhmaine
Guest
Posts: n/a
 
      01-20-2005
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">
<aspataList 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>
</aspataList>
<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
 
Reply With Quote
 
 
 
 
jmhmaine
Guest
Posts: n/a
 
      01-20-2005
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.Selected Index).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
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-21-2005
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.)

 
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
Checkboxlist inside Repeater - doesn't fire click event for Checkboxlist JD ASP .Net 5 08-08-2007 11:28 AM
Selected state not maintained on postback Santel ASP .Net Building Controls 3 06-13-2007 04:59 PM
Pop-up gives old values - like memory is not cleared and same values keep appearing chris ASP .Net 0 10-13-2006 11:57 AM
Session State not maintained asp.net 2.0 =?Utf-8?B?SXVsaWFu?= ASP .Net 2 03-28-2006 05:42 PM
CheckBoxList values not appearing and State not maintained when ne jmhmaine ASP .Net Web Controls 0 01-20-2005 04:25 PM



Advertisments