N
New User
I am trying to build a simple user control made up of a single
Repeater control and a bunch of dynamically created data entry
controls. The problem that is occurring however is that I am unable
to get the data back out of these controls on a postback so that I can
update my original dataset. I have a moved a paired down version of
the user control into a simple aspx page for posting here. Please
take a look at it and let me know if the events ar eout of order or if
there is another reason why it is not working. Thanks in advance.
The main issue is the fact that I cannot retrieve the data from the
controls in the "StorePageState" procedure. The FindControl function
returns nothing even though the controls do exist in the Repeater
Item. Thanks in advance.
--------------------------------
HTML FOR webforms1.aspx
--------------------------------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="TestPage.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PagingRepeater</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:repeater id="RepeaterEvaluationCriteria"
runat="server"></asp:repeater><asp:button id="Button1" runat="server"
Text="Postback"></asp:button></form>
</body>
</HTML>
--------------------------------
CODE BEHIND FOR webforms1.aspx
--------------------------------
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents RepeaterEvaluationCriteria As
System.Web.UI.WebControls.Repeater
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#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()
InitializeRepeaterTemplate()
End Sub
#End Region
Const cCheckBoxName As String = "chkBox"
Private Class TemplateRepeaterCriteria
Implements ITemplate
Dim TemplateType As ListItemType
Sub New(ByVal type As ListItemType)
TemplateType = type
End Sub
Private Sub TemplateControl_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)
Dim TableCellCriteria As HtmlTableCell
Dim CheckBoxItem As New CheckBox()
Dim Container As RepeaterItem
TableCellCriteria = CType(sender, HtmlTableCell)
Container = CType(TableCellCriteria.NamingContainer,
RepeaterItem)
CheckBoxItem.ID = cCheckBoxName
CheckBoxItem.Checked = DataBinder.Eval(Container.DataItem,
"selected")
TableCellCriteria.Controls.Add(CheckBoxItem)
End Sub
Sub InstantiateIn(ByVal Container As Control) Implements
ITemplate.InstantiateIn
Const cColumnSpan As String = "1"
Select Case TemplateType
Case ListItemType.Header
Dim TableHeaderCriteria As New Literal()
TableHeaderCriteria.Text = "<TABLE width='100%'
border='0'>"
TableHeaderCriteria.Text += "<TR><TD colspan='" &
cColumnSpan & "'><HR width='100%'></TD></TR>"
Container.Controls.Add(TableHeaderCriteria)
'Cleanup all Header Template objects
TableHeaderCriteria.Dispose()
TableHeaderCriteria = Nothing
Case ListItemType.Item
Dim TableRowCriteria As New HtmlTableRow()
Dim TableCellCriteria As New HtmlTableCell()
'Add Evaluation Item Label Table Cell
TableCellCriteria.NoWrap = True
AddHandler TableCellCriteria.DataBinding,
AddressOf TemplateControl_DataBinding
'Append Label to the Current Table Row
TableRowCriteria.Cells.Add(TableCellCriteria)
Container.Controls.Add(TableRowCriteria)
'Cleanup all Item Template Table Row and Cell
objects
TableRowCriteria.Dispose()
TableRowCriteria = Nothing
TableCellCriteria.Dispose()
TableCellCriteria = Nothing
Case ListItemType.Footer
Dim CriteriaTableFooter As New Literal()
CriteriaTableFooter.Text = "</TABLE>"
Container.Controls.Add(CriteriaTableFooter)
'Cleanup all Footer Template objects
CriteriaTableFooter.Dispose()
CriteriaTableFooter = Nothing
End Select
End Sub
End Class
Sub InitializeRepeaterTemplate()
'Call constructor to build the custom Header template for the
Evaluation Criteria Repeater
RepeaterEvaluationCriteria.HeaderTemplate = New
TemplateRepeaterCriteria(ListItemType.Header)
'Call constructor to build the custom Item (row) template for
the Evaluation Criteria Repeater
RepeaterEvaluationCriteria.ItemTemplate = New
TemplateRepeaterCriteria(ListItemType.Item)
'Call constructor to build the custom Footer template for the
Evaluation Criteria Repeater
RepeaterEvaluationCriteria.FooterTemplate = New
TemplateRepeaterCriteria(ListItemType.Footer)
End Sub
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 IsPostBack) Then
Load_Data()
Else
StorePageState()
End If
Bind_Repeater_Data()
End Sub
Private Sub Load_Data()
Dim tb As DataTable = New DataTable()
tb.Columns.Add("selected")
Dim index As Integer = 0
Dim NewRow As DataRow
Dim i As Integer
For i = 0 To 10
index = i + 1
NewRow = tb.NewRow()
If (i Mod 2 = 0) Then
NewRow("selected") = True
Else
NewRow("selected") = False
End If
tb.Rows.Add(NewRow)
Next
ViewState("TEMP_TABLE") = tb
End Sub
Private Sub Bind_Repeater_Data()
If IsNothing(ViewState("TEMP_TABLE")) Then
Load_Data()
End If
Dim tb As DataTable = CType(ViewState("TEMP_TABLE"),
DataTable)
Dim ttb As DataTable = tb.Clone()
Dim recordcount As Integer = tb.Rows.Count
Dim i As Integer
For i = 0 To tb.Rows.Count - 1
ttb.ImportRow(tb.Rows(i))
Next
RepeaterEvaluationCriteria.DataSource = ttb
RepeaterEvaluationCriteria.DataBind()
End Sub
Private Sub StorePageState()
Dim tb As DataTable = CType(ViewState("TEMP_TABLE"),
DataTable)
Dim chk As CheckBox
Dim row As DataRow
Dim i As Integer
For i = 0 To RepeaterEvaluationCriteria.Items.Count - 1
'PROBLEM HERE!!!!
chk = CType(RepeaterEvaluationCriteria.Items(i).FindControl(cCheckBoxName),
CheckBox)
row = tb.Rows(i)
row("selected") = chk.Checked
Next
End Sub
End Class
Repeater control and a bunch of dynamically created data entry
controls. The problem that is occurring however is that I am unable
to get the data back out of these controls on a postback so that I can
update my original dataset. I have a moved a paired down version of
the user control into a simple aspx page for posting here. Please
take a look at it and let me know if the events ar eout of order or if
there is another reason why it is not working. Thanks in advance.
The main issue is the fact that I cannot retrieve the data from the
controls in the "StorePageState" procedure. The FindControl function
returns nothing even though the controls do exist in the Repeater
Item. Thanks in advance.
--------------------------------
HTML FOR webforms1.aspx
--------------------------------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="TestPage.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PagingRepeater</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:repeater id="RepeaterEvaluationCriteria"
runat="server"></asp:repeater><asp:button id="Button1" runat="server"
Text="Postback"></asp:button></form>
</body>
</HTML>
--------------------------------
CODE BEHIND FOR webforms1.aspx
--------------------------------
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents RepeaterEvaluationCriteria As
System.Web.UI.WebControls.Repeater
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#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()
InitializeRepeaterTemplate()
End Sub
#End Region
Const cCheckBoxName As String = "chkBox"
Private Class TemplateRepeaterCriteria
Implements ITemplate
Dim TemplateType As ListItemType
Sub New(ByVal type As ListItemType)
TemplateType = type
End Sub
Private Sub TemplateControl_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)
Dim TableCellCriteria As HtmlTableCell
Dim CheckBoxItem As New CheckBox()
Dim Container As RepeaterItem
TableCellCriteria = CType(sender, HtmlTableCell)
Container = CType(TableCellCriteria.NamingContainer,
RepeaterItem)
CheckBoxItem.ID = cCheckBoxName
CheckBoxItem.Checked = DataBinder.Eval(Container.DataItem,
"selected")
TableCellCriteria.Controls.Add(CheckBoxItem)
End Sub
Sub InstantiateIn(ByVal Container As Control) Implements
ITemplate.InstantiateIn
Const cColumnSpan As String = "1"
Select Case TemplateType
Case ListItemType.Header
Dim TableHeaderCriteria As New Literal()
TableHeaderCriteria.Text = "<TABLE width='100%'
border='0'>"
TableHeaderCriteria.Text += "<TR><TD colspan='" &
cColumnSpan & "'><HR width='100%'></TD></TR>"
Container.Controls.Add(TableHeaderCriteria)
'Cleanup all Header Template objects
TableHeaderCriteria.Dispose()
TableHeaderCriteria = Nothing
Case ListItemType.Item
Dim TableRowCriteria As New HtmlTableRow()
Dim TableCellCriteria As New HtmlTableCell()
'Add Evaluation Item Label Table Cell
TableCellCriteria.NoWrap = True
AddHandler TableCellCriteria.DataBinding,
AddressOf TemplateControl_DataBinding
'Append Label to the Current Table Row
TableRowCriteria.Cells.Add(TableCellCriteria)
Container.Controls.Add(TableRowCriteria)
'Cleanup all Item Template Table Row and Cell
objects
TableRowCriteria.Dispose()
TableRowCriteria = Nothing
TableCellCriteria.Dispose()
TableCellCriteria = Nothing
Case ListItemType.Footer
Dim CriteriaTableFooter As New Literal()
CriteriaTableFooter.Text = "</TABLE>"
Container.Controls.Add(CriteriaTableFooter)
'Cleanup all Footer Template objects
CriteriaTableFooter.Dispose()
CriteriaTableFooter = Nothing
End Select
End Sub
End Class
Sub InitializeRepeaterTemplate()
'Call constructor to build the custom Header template for the
Evaluation Criteria Repeater
RepeaterEvaluationCriteria.HeaderTemplate = New
TemplateRepeaterCriteria(ListItemType.Header)
'Call constructor to build the custom Item (row) template for
the Evaluation Criteria Repeater
RepeaterEvaluationCriteria.ItemTemplate = New
TemplateRepeaterCriteria(ListItemType.Item)
'Call constructor to build the custom Footer template for the
Evaluation Criteria Repeater
RepeaterEvaluationCriteria.FooterTemplate = New
TemplateRepeaterCriteria(ListItemType.Footer)
End Sub
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 IsPostBack) Then
Load_Data()
Else
StorePageState()
End If
Bind_Repeater_Data()
End Sub
Private Sub Load_Data()
Dim tb As DataTable = New DataTable()
tb.Columns.Add("selected")
Dim index As Integer = 0
Dim NewRow As DataRow
Dim i As Integer
For i = 0 To 10
index = i + 1
NewRow = tb.NewRow()
If (i Mod 2 = 0) Then
NewRow("selected") = True
Else
NewRow("selected") = False
End If
tb.Rows.Add(NewRow)
Next
ViewState("TEMP_TABLE") = tb
End Sub
Private Sub Bind_Repeater_Data()
If IsNothing(ViewState("TEMP_TABLE")) Then
Load_Data()
End If
Dim tb As DataTable = CType(ViewState("TEMP_TABLE"),
DataTable)
Dim ttb As DataTable = tb.Clone()
Dim recordcount As Integer = tb.Rows.Count
Dim i As Integer
For i = 0 To tb.Rows.Count - 1
ttb.ImportRow(tb.Rows(i))
Next
RepeaterEvaluationCriteria.DataSource = ttb
RepeaterEvaluationCriteria.DataBind()
End Sub
Private Sub StorePageState()
Dim tb As DataTable = CType(ViewState("TEMP_TABLE"),
DataTable)
Dim chk As CheckBox
Dim row As DataRow
Dim i As Integer
For i = 0 To RepeaterEvaluationCriteria.Items.Count - 1
'PROBLEM HERE!!!!
chk = CType(RepeaterEvaluationCriteria.Items(i).FindControl(cCheckBoxName),
CheckBox)
row = tb.Rows(i)
row("selected") = chk.Checked
Next
End Sub
End Class