J
jmhmaine
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 set. 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
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 set. 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