Repeater - ItemDataBound and ItemCreated

Repeater - ItemDataBound and ItemCreated

am 18.11.2005 16:53:16 von djone2

I'm creating a dynamic control for each row in a REPEATER based on
database values. ItemDataBound creates the control for the initial load
(not postback), but I cannot get the control recreated when command to
save data (postback) occurs. How do I get the values for this dynamic
control on postback and place the control back into its original
position?

Sample Uses Pubs Database for SQL Server:

Imports System.Data.SqlClient

Public Class FullyEditableRepeater
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
Private Sub
InitializeComponent()

End Sub
Protected WithEvents rpResults1 As
System.Web.UI.WebControls.Repeater
Protected WithEvents lblUpdateResults As
System.Web.UI.WebControls.Label
Protected WithEvents btnUpdateAll As
System.Web.UI.WebControls.Button
Protected WithEvents lblPageMode As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

'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 connectionstring As String =
"Server=VX72998;Database=pubs;User=sa;Password=;"


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles
MyBase.Load

If Not Page.IsPostBack Then
lblPageMode.Text = "First Request..."
BindData()
Else
lblPageMode.Text = "Post Back..."
End If
End Sub


Sub BindData()

Const strSQL As String = "SELECT pub_id, pub_name, city, state,
country FROM publishers"

Dim myConnection As New SqlConnection(connectionstring)
Dim myCommand As New SqlCommand(strSQL, myConnection)

myConnection.Open()
rpResults1.DataSource = myCommand.ExecuteReader()
rpResults1.DataBind()
myConnection.Close()

End Sub

Private Sub btnUpdateAll_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnUpdateAll.Click

Dim dgi As RepeaterItem
Dim updateSQL As String = "UPDATE publishers SET pub_name =
@pub_name WHERE pub_id = @pub_id"
Dim myConnection As New SqlConnection(connectionstring)
Dim myCommand As New SqlCommand(updateSQL, myConnection)

myConnection.Open()

For Each dgi In rpResults1.Items

'Read in the Primary Key Field
Dim pub_id As Integer = CType(dgi.FindControl("pub_id"),
TextBox).Text
Dim pub_name As String = CType(dgi.FindControl("pub_name"),
TextBox).Text
Dim _comments As Control = dgi.FindControl("comments")
Dim comments As String


If Not _comments Is Nothing Then
comments = CType(dgi.FindControl("comments"),
TextBox).Text
Response.Write("Comments Found
")
Else
Response.Write("No Comments Found
")
comments = "**************************"
End If

'Issue an UPDATE statement...
myCommand.Parameters.Clear()
myCommand.Parameters.Add("@pub_name", pub_name)
myCommand.Parameters.Add("@pub_id", pub_id)
Response.Write("Pub Name: " + pub_name.ToString + "
")
Response.Write("Pub ID: " + pub_id.ToString + "
")
Response.Write("Comments: " + comments.ToString + "
")
myCommand.ExecuteNonQuery()

Next

myConnection.Close()

lblUpdateResults.Text = "Rows Updated..."


End Sub

Private Sub dgPopularFAQs_ItemDataBound(ByVal sender As Object,
ByVal e As RepeaterItemEventArgs) Handles rpResults1.ItemDataBound

If e.Item.ItemType = ListItemType.AlternatingItem Or
e.Item.ItemType = ListItemType.Item Then


If e.Item.DataItem("pub_id") = "9901" Then


Dim textAreaCell As HtmlTableCell
Dim ratingCell4 As HtmlTableCell
Dim ratingCell5 As HtmlTableCell

textAreaCell = CType(e.Item.FindControl("Td13"),
HtmlTableCell)
ratingCell4 = CType(e.Item.FindControl("Td14"),
HtmlTableCell)
ratingCell5 = CType(e.Item.FindControl("Td15"),
HtmlTableCell)

Dim commentsField As New TextBox
commentsField.TextMode = TextBoxMode.MultiLine
commentsField.Wrap = True
commentsField.Rows = 3
commentsField.Columns = 55
commentsField.ID = "comments"
commentsField.CssClass = "TEXTAREA"
ratingCell4.Visible = False
ratingCell5.Visible = False
textAreaCell.Controls.Clear()
textAreaCell.ColSpan = "3"
textAreaCell.Align = "left"
textAreaCell.Controls.Add(commentsField)

'set flag for current row where dynamic control was
added
ViewState("CommentsIndex" + e.Item.ItemIndex.ToString) =
"Y"



End If


End If

End Sub


Private Sub dgPopularFAQs_ItemCreated(ByVal sender As Object, ByVal
e As RepeaterItemEventArgs) Handles rpResults1.ItemCreated

If e.Item.ItemType = ListItemType.AlternatingItem Or
e.Item.ItemType = ListItemType.Item Then

'check flag for current row where dynamic control was added
If ViewState("CommentsIndex" + e.Item.ItemIndex.ToString) =
"Y" Then


Dim textAreaCell As HtmlTableCell
Dim ratingCell4 As HtmlTableCell
Dim ratingCell5 As HtmlTableCell

textAreaCell = CType(e.Item.FindControl("Td13"),
HtmlTableCell)
ratingCell4 = CType(e.Item.FindControl("Td14"),
HtmlTableCell)
ratingCell5 = CType(e.Item.FindControl("Td15"),
HtmlTableCell)

Dim commentsField As New TextBox
commentsField.TextMode = TextBoxMode.MultiLine
commentsField.Wrap = True
commentsField.Rows = 3
commentsField.Columns = 55
commentsField.ID = "comments"
commentsField.CssClass = "TEXTAREA"
ratingCell4.Visible = False
ratingCell5.Visible = False
textAreaCell.Controls.Clear()
textAreaCell.ColSpan = "3"
textAreaCell.Align = "left"
textAreaCell.Controls.Add(commentsField)

End If

End If


End Sub


End Class


==============================================
ASPX Code
==============================================




cellSpacing="0" cellPadding="3"
border="1" bordercolor="#000000" id="tableDetails1" width="650">



















Pub ID

Pub Name

text='<%# Container.DataItem("pub_id") %>'>

text='<%# Container.DataItem("pub_name") %>'>

GroupName="rating_value1">

GroupName="rating_value1">

GroupName="rating_value1">









*** Sent via Developersdex http://www.developersdex.com ***

Re: Repeater - ItemDataBound and ItemCreated

am 18.11.2005 17:27:25 von Brock Allen

You can handle the ItemCreated event and repop the dynamic controls there
when it's a postback. The only problem is that your dynamic controls depend
upon the data from the original datasource, so you're going to have to remember
across postbacks yourself what those dynamic controls were.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> I'm creating a dynamic control for each row in a REPEATER based on
> database values. ItemDataBound creates the control for the initial
> load (not postback), but I cannot get the control recreated when
> command to save data (postback) occurs. How do I get the values for
> this dynamic control on postback and place the control back into its
> original position?
>
> Sample Uses Pubs Database for SQL Server:
>
> Imports System.Data.SqlClient
>
> Public Class FullyEditableRepeater
> Inherits System.Web.UI.Page
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> Private Sub
> InitializeComponent()
> End Sub
> Protected WithEvents rpResults1 As
> System.Web.UI.WebControls.Repeater
> Protected WithEvents lblUpdateResults As
> System.Web.UI.WebControls.Label
> Protected WithEvents btnUpdateAll As
> System.Web.UI.WebControls.Button
> Protected WithEvents lblPageMode As
> System.Web.UI.WebControls.Label
> Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
> '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 connectionstring As String =
> "Server=VX72998;Database=pubs;User=sa;Password=;"
> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
> Handles MyBase.Load
>
> If Not Page.IsPostBack Then
> lblPageMode.Text = "First Request..."
> BindData()
> Else
> lblPageMode.Text = "Post Back..."
> End If
> End Sub
> Sub BindData()
>
> Const strSQL As String = "SELECT pub_id, pub_name, city,
> state, country FROM publishers"
>
> Dim myConnection As New SqlConnection(connectionstring)
> Dim myCommand As New SqlCommand(strSQL, myConnection)
> myConnection.Open()
> rpResults1.DataSource = myCommand.ExecuteReader()
> rpResults1.DataBind()
> myConnection.Close()
> End Sub
>
> Private Sub btnUpdateAll_Click(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles btnUpdateAll.Click
>
> Dim dgi As RepeaterItem
> Dim updateSQL As String = "UPDATE publishers SET pub_name =
> @pub_name WHERE pub_id = @pub_id"
> Dim myConnection As New SqlConnection(connectionstring)
> Dim myCommand As New SqlCommand(updateSQL, myConnection)
> myConnection.Open()
>
> For Each dgi In rpResults1.Items
>
> 'Read in the Primary Key Field
> Dim pub_id As Integer = CType(dgi.FindControl("pub_id"),
> TextBox).Text
> Dim pub_name As String =
> CType(dgi.FindControl("pub_name"),
> TextBox).Text
> Dim _comments As Control = dgi.FindControl("comments")
> Dim comments As String
> If Not _comments Is Nothing Then
> comments = CType(dgi.FindControl("comments"),
> TextBox).Text
> Response.Write("Comments Found
")
> Else
> Response.Write("No Comments Found
")
> comments = "**************************"
> End If
> 'Issue an UPDATE statement...
> myCommand.Parameters.Clear()
> myCommand.Parameters.Add("@pub_name", pub_name)
> myCommand.Parameters.Add("@pub_id", pub_id)
> Response.Write("Pub Name: " + pub_name.ToString + "
")
> Response.Write("Pub ID: " + pub_id.ToString + "
")
> Response.Write("Comments: " + comments.ToString + "
")
> myCommand.ExecuteNonQuery()
> Next
>
> myConnection.Close()
>
> lblUpdateResults.Text = "Rows Updated..."
>
> End Sub
>
> Private Sub dgPopularFAQs_ItemDataBound(ByVal sender As Object,
> ByVal e As RepeaterItemEventArgs) Handles rpResults1.ItemDataBound
>
> If e.Item.ItemType = ListItemType.AlternatingItem Or
> e.Item.ItemType = ListItemType.Item Then
>
> If e.Item.DataItem("pub_id") = "9901" Then
>
> Dim textAreaCell As HtmlTableCell
> Dim ratingCell4 As HtmlTableCell
> Dim ratingCell5 As HtmlTableCell
> textAreaCell = CType(e.Item.FindControl("Td13"),
> HtmlTableCell)
> ratingCell4 = CType(e.Item.FindControl("Td14"),
> HtmlTableCell)
> ratingCell5 = CType(e.Item.FindControl("Td15"),
> HtmlTableCell)
> Dim commentsField As New TextBox
> commentsField.TextMode = TextBoxMode.MultiLine
> commentsField.Wrap = True
> commentsField.Rows = 3
> commentsField.Columns = 55
> commentsField.ID = "comments"
> commentsField.CssClass = "TEXTAREA"
> ratingCell4.Visible = False
> ratingCell5.Visible = False
> textAreaCell.Controls.Clear()
> textAreaCell.ColSpan = "3"
> textAreaCell.Align = "left"
> textAreaCell.Controls.Add(commentsField)
> 'set flag for current row where dynamic control was
> added
> ViewState("CommentsIndex" + e.Item.ItemIndex.ToString)
> =
> "Y"
> End If
>
> End If
>
> End Sub
>
> Private Sub dgPopularFAQs_ItemCreated(ByVal sender As Object,
> ByVal e As RepeaterItemEventArgs) Handles rpResults1.ItemCreated
>
> If e.Item.ItemType = ListItemType.AlternatingItem Or
> e.Item.ItemType = ListItemType.Item Then
>
> 'check flag for current row where dynamic control was
> added
> If ViewState("CommentsIndex" + e.Item.ItemIndex.ToString)
> =
> "Y" Then
> Dim textAreaCell As HtmlTableCell
> Dim ratingCell4 As HtmlTableCell
> Dim ratingCell5 As HtmlTableCell
> textAreaCell = CType(e.Item.FindControl("Td13"),
> HtmlTableCell)
> ratingCell4 = CType(e.Item.FindControl("Td14"),
> HtmlTableCell)
> ratingCell5 = CType(e.Item.FindControl("Td15"),
> HtmlTableCell)
> Dim commentsField As New TextBox
> commentsField.TextMode = TextBoxMode.MultiLine
> commentsField.Wrap = True
> commentsField.Rows = 3
> commentsField.Columns = 55
> commentsField.ID = "comments"
> commentsField.CssClass = "TEXTAREA"
> ratingCell4.Visible = False
> ratingCell5.Visible = False
> textAreaCell.Controls.Clear()
> textAreaCell.ColSpan = "3"
> textAreaCell.Align = "left"
> textAreaCell.Controls.Add(commentsField)
> End If
>
> End If
>
> End Sub
>
> End Class
>
> ==============================================
> ASPX Code
> ==============================================
>
>
>

> cellSpacing="0" cellPadding="3"
> border="1" bordercolor="#000000" id="tableDetails1" width="650">
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>

> Pub ID
>

> Pub Name
>

> > text='<%# Container.DataItem("pub_id") %>'>
>

> > text='<%# Container.DataItem("pub_name") %>'>
>

> > GroupName="rating_value1">

> > GroupName="rating_value1">

> > GroupName="rating_value1">

>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

Re: Repeater - ItemDataBound and ItemCreated

am 18.11.2005 17:41:16 von djone2

I'm using ViewState to indicate which row has the dynamic control. See
ItemCreated:

'check flag for current row where dynamic control was added
If ViewState("CommentsIndex" + e.Item.ItemIndex.ToString) = "Y" Then

...add control back


End if






*** Sent via Developersdex http://www.developersdex.com ***

Re: Repeater - ItemDataBound and ItemCreated

am 18.11.2005 17:45:00 von Brock Allen

Ah, well then you should be all set then to repop the RepeaterItem from ItemCreated
then :)

-Brock
DevelopMentor
http://staff.develop.com/ballen

> I'm using ViewState to indicate which row has the dynamic control.
> See ItemCreated:
>
> 'check flag for current row where dynamic control was added If
> ViewState("CommentsIndex" + e.Item.ItemIndex.ToString) = "Y" Then
>
> ..add control back
>
> End if
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

Re: Repeater - ItemDataBound and ItemCreated

am 18.11.2005 18:45:15 von djone2

The repeater returns the correct controls on postback, but values for
the dynamic controls are lost. What's missing?

If I change ItemCreated TO e.Item.Controls.Add
(commentsField) FROM textAreaCell.Controls.Add(commentsField), the
dynamic control retains the value on postback, but appears outside the
html table cell, which is where i need to control to recreate.




*** Sent via Developersdex http://www.developersdex.com ***