Repeater - ItemDataBound and ItemCreated

Discussion in 'ASP .Net' started by Dennis E. Jones, Jr., Nov 18, 2005.

  1. 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., Nov 18, 2005
    #1
    1. Advertising

  2. Dennis E. Jones, Jr.

    Brock Allen Guest

    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 ***
    >
     
    Brock Allen, Nov 18, 2005
    #2
    1. Advertising

  3. 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., Nov 18, 2005
    #3
  4. Dennis E. Jones, Jr.

    Brock Allen Guest

    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 ***
    >
     
    Brock Allen, Nov 18, 2005
    #4
  5. 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 ***
     
    Dennis E. Jones, Jr., Nov 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2V2aW4gSGVjdG9y?=

    Repeater ItemCreated vs ItemDataBound

    =?Utf-8?B?S2V2aW4gSGVjdG9y?=, Apr 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    10,041
    Teemu Keiski
    Apr 11, 2004
  2. Replies:
    3
    Views:
    1,919
  3. Philippe Camus
    Replies:
    2
    Views:
    2,521
    Philippe Camus
    Mar 30, 2006
  4. Replies:
    3
    Views:
    518
    Eliyahu Goldin
    Sep 20, 2007
  5. Thomas Nielsen [AM Production A/S]

    Repeater/ViewState: how to preserve properties on controls set on ItemCreated?

    Thomas Nielsen [AM Production A/S], Dec 1, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    374
    Thomas Nielsen [AM Production A/S]
    Dec 2, 2003
Loading...

Share This Page