ASPNET: Composite control: When page do postback, repeater is losing bound datas.

When i click on eventTest button. Repeater is losing bound datas. And my codes is here.

ASPX:
<asp:Button ID="eventTest" runat="server" Text="Event Test" />

Open in new window


ASPX.VB
Partial Class forExpert
    Inherits System.Web.UI.Page
    Public WithEvents myCntrl As New myControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            myCntrl.ID = "myCntrl"
            Page.Form.Controls.Add(myCntrl)
            myCntrl.DataBind()
        End If
    End Sub
End Class

Open in new window


CompositeControl
Public Class myControl
    Inherits CompositeControl
    Public WithEvents rptr As New Repeater

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        rptr.ID = Me.ID & "rptr"
        rptr.ItemTemplate = New rptrITemp

        Me.Controls.Add(New LiteralControl("<table>"))
        Me.Controls.Add(rptr)
        Me.Controls.Add(New LiteralControl("</table>"))

    End Sub

    Public Overrides Sub DataBind()
        MyBase.DataBind()
        Dim dTable As New DataTable
        dTable.Columns.Add("col1")
        dTable.Columns.Add("col2")

        Dim dRow As DataRow = dTable.NewRow
        dRow(0) = "Ahmet" : dRow(1) = "-[125]-"

        Dim dRow1 As DataRow = dTable.NewRow
        dRow1(0) = "Fikret" : dRow1(1) = "-[174]-"

        Dim dRow2 As DataRow = dTable.NewRow
        dRow2(0) = "Hüseyin" : dRow2(1) = "-[113]-"

        dTable.Rows.Add(dRow)
        dTable.Rows.Add(dRow1)
        dTable.Rows.Add(dRow2)

        rptr.DataSource = dTable
        rptr.DataBind()
    End Sub

    Private Class rptrITemp
        Implements ITemplate

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
            Dim pHolder As New PlaceHolder
            AddHandler pHolder.DataBinding, AddressOf pHolder_DataBinding
            container.Controls.Add(pHolder)
        End Sub

        Private Sub pHolder_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim pHolder As PlaceHolder = CType(sender, PlaceHolder)
            Dim rItem As RepeaterItem = CType(pHolder.NamingContainer, RepeaterItem)
            pHolder.Controls.Add(New LiteralControl("<tr>"))
            For index = 0 To DirectCast(rItem.DataItem, DataRowView).Row.ItemArray.Count - 1
                pHolder.Controls.Add(New LiteralControl(String.Format("<td valign='top' align='left'>{0}</td>", _
                                  DirectCast(rItem.DataItem, DataRowView).Row(index).ToString)))
                '-*-*-*-*-*-*-*-*AutoGenerateMenu
            Next
            pHolder.Controls.Add(New LiteralControl("</tr>"))

        End Sub

    End Class
End Class

Open in new window

ztekinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ingriTCommented:
You only perform the databound when there is NO postback, so it is executed once. If the button is not inside an updatepanel, then the whole page is updated and so is the databound repeater, but you don't reset the data bind on postback, so it is empty.

What are you trying to achieve? Is it an option to always perform the databind, so to remove the If IsPostBack = False Then?

Or could you add a parameter to the button, to not perform a postback (AutoPostBack="false")?
0
RickCommented:
On postback, you need to recall the method that populates your repeater.
0
ztekinAuthor Commented:
I guess I couldn't tell the my problem. I don't want to databind my compositecontrol on every postback. I want: If I bound datas one time, repeater has to able to save datas like TextBox.

I don't know: How can i save viewstate of repeater and load them.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ingriTCommented:
0
ztekinAuthor Commented:
hi ingriT,

I tried it like that, but result didn't change.

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        rptr.ID = Me.ID & "rptr"
        rptr.ItemTemplate = New rptrITemp

        Me.Controls.Add(New LiteralControl("<table>"))
        Me.Controls.Add(rptr)
        Me.Controls.Add(New LiteralControl("</table>"))
        DataBind() ' <--*-*-*-*-*-*-* LIKE THAT
    End Sub

Open in new window

0
ingriTCommented:
What are you databinding with the DataBind() call? I don't see you binding the repeater like rptr.DataBind() or something?
0
ztekinAuthor Commented:
line 37 in CompositeControl

        dTable.Rows.Add(dRow1)
        dTable.Rows.Add(dRow2)

        rptr.DataSource = dTable
        rptr.DataBind() ' <---*-*-*-*-*-*
    End Sub

    Private Class rptrITemp
        Implements ITemplate

Open in new window

0
ingriTCommented:
Is it still loaded correctly the first time (so before the postback) when you only do the databind OnInit()?
You have removed it from the PageLoad right?
0
ztekinAuthor Commented:
Yes I have removed it PageLoad.
0
ztekinAuthor Commented:
But, after the postback I lost datas again.
0
ztekinAuthor Commented:
Aren't there any solution for that...
0
ingriTCommented:
@CodeCruiser: You just gave the exact link I posted earlier, but that doesn't seem to work.

@ztekin: Could you post the entire page code again, but now with the databinding in the Init method?
0
ztekinAuthor Commented:
Hi Friends,

Thanks all of your answers. I solved my problem. And last situation of my code is here.

Solution: Repeater is saving viewstate just for control that you created in InstantiateIn. The control created in the DataBinding Event can't save own datas. ( Line 49 in CompositeControl )


ASPX:
<asp:Button ID="eventTest" runat="server" Text="Event Test" />

Open in new window


ASPX.VB:
Partial Class forExpert
    Inherits System.Web.UI.Page
    Public WithEvents myCntrl As New myControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        myCntrl.ID = "myCntrl"
        Me.Controls.Add(myCntrl)
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            myCntrl.DataBind()
        End If
    End Sub
End Class

Open in new window


CompositeControl
Public Class myControl
    Inherits CompositeControl
    Public WithEvents rptr As New Repeater
    Public WithEvents pHold As New PlaceHolder

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        With rptr
            .ID = Me.ID & "rptr"
        End With
        rptr.ItemTemplate = New rptrITemp
        pHold.ID = "pHold"

        Me.Controls.Add(New LiteralControl("<table>"))
        Me.Controls.Add(rptr)
        Me.Controls.Add(New LiteralControl("</table>"))
        MyBase.OnInit(e)
    End Sub

    Public Overrides Sub DataBind()
        MyBase.DataBind()
        Dim dTable As New DataTable
        dTable.Columns.Add("col1")
        dTable.Columns.Add("col2")

        Dim dRow As DataRow = dTable.NewRow
        dRow(0) = "Ahmet" : dRow(1) = "-[125]-"

        Dim dRow1 As DataRow = dTable.NewRow
        dRow1(0) = "Fikret" : dRow1(1) = "-[174]-"

        Dim dRow2 As DataRow = dTable.NewRow
        dRow2(0) = "Hüseyin" : dRow2(1) = "-[113]-"

        dTable.Rows.Add(dRow)
        dTable.Rows.Add(dRow1)
        dTable.Rows.Add(dRow2)

        rptr.DataSource = dTable
        rptr.DataBind()

    End Sub

    Private Class rptrITemp
        Implements ITemplate

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
            Dim pHolder As New PlaceHolder

            Dim tableTR As New Literal ' <---*-*-*-*-*
            tableTR.ID = "tableTR"

            pHolder.Controls.Add(tableTR)

            AddHandler pHolder.DataBinding, AddressOf pHolder_DataBinding
            container.Controls.Add(pHolder)
        End Sub

        Private Sub pHolder_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim pHolder As PlaceHolder = CType(sender, PlaceHolder)
            Dim rItem As RepeaterItem = CType(pHolder.NamingContainer, RepeaterItem)
            Dim htmlText As String = ""

            htmlText = "<tr>"

            For index = 0 To DirectCast(rItem.DataItem, DataRowView).Row.ItemArray.Count - 1
                htmlText = htmlText & String.Format("<td valign='top' align='left'>{0}</td>", _
                                                                                DirectCast(rItem.DataItem, DataRowView).Row(index).ToString)
                '-*-*-*-*-*-*-*-*AutoGenerateMenu
            Next
            htmlText = htmlText & "</tr>"
            CType(pHolder.FindControl("tableTR"), Literal).Text = htmlText

        End Sub

    End Class
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
@ingriT
Did not mean to. Did not notice that link.
0
ztekinAuthor Commented:
There is open code and explanation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.