Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Repeater - ItemDataBound and ItemCreated (http://www.velocityreviews.com/forums/t113833-repeater-itemdatabound-and-itemcreated.html)

Dennis E. Jones, Jr. 11-18-2005 03:53 PM

Repeater - ItemDataBound and ItemCreated
 
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.
<System.Diagnostics.DebuggerStepThrough()> 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<br>")
Else
Response.Write("No Comments Found<br>")
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 + "<br>")
Response.Write("Pub ID: " + pub_id.ToString + "<br>")
Response.Write("Comments: " + comments.ToString + "<br>")
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
==============================================


<asp:repeater id="rpResults1" runat="server">
<headertemplate>
<table style="BORDER-COLLAPSE: collapse; margin-left:2"
cellSpacing="0" cellPadding="3"
border="1" bordercolor="#000000" id="tableDetails1" width="650">
<thead>
<tr>
<th align="left" valign="bottom">
<strong>Pub ID</strong>
</th>
<th align="left" valign="bottom">
<strong>Pub Name</strong>
</th>
</tr>
</thead>
<tbody>
</headertemplate>
<itemtemplate>
<tr id="Tr11" runat="server" bgcolor="#ffffff">
<td id="Td11" runat="server" valign="top" align="left">
<asp:TextBox ID="pub_id" TextMode="SingleLine" Runat="server"
text='<%# Container.DataItem("pub_id") %>'>
</asp:TextBox></td>
<td id="Td12" runat="server" valign="top" align="left">
<asp:textbox ID="pub_name" TextMode="SingleLine" Runat="server"
text='<%# Container.DataItem("pub_name") %>'>
</asp:textbox></td>
<td id="Td13" runat="server" valign="middle" align="center">
<asp:RadioButton id="rbRating15" runat="server"
GroupName="rating_value1"></asp:RadioButton></td>
<td id="Td14" runat="server" valign="middle" align="center">
<asp:RadioButton id="rbRating13" runat="server"
GroupName="rating_value1"></asp:RadioButton></td>
<td id="Td15" runat="server" valign="middle" align="center">
<asp:RadioButton id="rbRating11" runat="server"
GroupName="rating_value1"></asp:RadioButton></td>
</tr>
</itemtemplate>
<footertemplate>
</tbody> </table>
</footertemplate>
</asp:repeater>
<asp:button id="btnUpdateAll" runat="server" text="Update
All"></asp:button>





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

Brock Allen 11-18-2005 04:27 PM

Re: Repeater - ItemDataBound and ItemCreated
 
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.
> <System.Diagnostics.DebuggerStepThrough()> 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<br>")
> Else
> Response.Write("No Comments Found<br>")
> 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 + "<br>")
> Response.Write("Pub ID: " + pub_id.ToString + "<br>")
> Response.Write("Comments: " + comments.ToString + "<br>")
> 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
> ==============================================
> <asp:repeater id="rpResults1" runat="server">
> <headertemplate>
> <table style="BORDER-COLLAPSE: collapse; margin-left:2"
> cellSpacing="0" cellPadding="3"
> border="1" bordercolor="#000000" id="tableDetails1" width="650">
> <thead>
> <tr>
> <th align="left" valign="bottom">
> <strong>Pub ID</strong>
> </th>
> <th align="left" valign="bottom">
> <strong>Pub Name</strong>
> </th>
> </tr>
> </thead>
> <tbody>
> </headertemplate>
> <itemtemplate>
> <tr id="Tr11" runat="server" bgcolor="#ffffff">
> <td id="Td11" runat="server" valign="top" align="left">
> <asp:TextBox ID="pub_id" TextMode="SingleLine" Runat="server"
> text='<%# Container.DataItem("pub_id") %>'>
> </asp:TextBox></td>
> <td id="Td12" runat="server" valign="top" align="left">
> <asp:textbox ID="pub_name" TextMode="SingleLine" Runat="server"
> text='<%# Container.DataItem("pub_name") %>'>
> </asp:textbox></td>
> <td id="Td13" runat="server" valign="middle" align="center">
> <asp:RadioButton id="rbRating15" runat="server"
> GroupName="rating_value1"></asp:RadioButton></td>
> <td id="Td14" runat="server" valign="middle" align="center">
> <asp:RadioButton id="rbRating13" runat="server"
> GroupName="rating_value1"></asp:RadioButton></td>
> <td id="Td15" runat="server" valign="middle" align="center">
> <asp:RadioButton id="rbRating11" runat="server"
> GroupName="rating_value1"></asp:RadioButton></td>
> </tr>
> </itemtemplate>
> <footertemplate>
> </tbody> </table>
> </footertemplate>
> </asp:repeater>
> <asp:button id="btnUpdateAll" runat="server" text="Update
> All"></asp:button>
> *** Sent via Developersdex http://www.developersdex.com ***
>




Dennis E. Jones, Jr. 11-18-2005 04:41 PM

Re: Repeater - ItemDataBound and ItemCreated
 
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 ***

Brock Allen 11-18-2005 04:45 PM

Re: Repeater - ItemDataBound and ItemCreated
 
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 ***
>




Dennis E. Jones, Jr. 11-18-2005 05:45 PM

Re: Repeater - ItemDataBound and ItemCreated
 
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 ***


All times are GMT. The time now is 06:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.