Solved

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

Posted on 2012-03-23
16
1,018 Views
Last Modified: 2012-03-31
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

0
Comment
Question by:ztekin
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 6

Expert Comment

by:ingriT
ID: 37757460
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
 
LVL 13

Expert Comment

by:Rick
ID: 37757519
On postback, you need to recall the method that populates your repeater.
0
 

Author Comment

by:ztekin
ID: 37757548
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
 
LVL 6

Expert Comment

by:ingriT
ID: 37757564
0
 

Author Comment

by:ztekin
ID: 37757635
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
 
LVL 6

Expert Comment

by:ingriT
ID: 37757644
What are you databinding with the DataBind() call? I don't see you binding the repeater like rptr.DataBind() or something?
0
 

Author Comment

by:ztekin
ID: 37757660
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
 
LVL 6

Expert Comment

by:ingriT
ID: 37757674
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:ztekin
ID: 37757738
Yes I have removed it PageLoad.
0
 

Author Comment

by:ztekin
ID: 37757745
But, after the postback I lost datas again.
0
 

Author Comment

by:ztekin
ID: 37764501
Aren't there any solution for that...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37765102
0
 
LVL 6

Expert Comment

by:ingriT
ID: 37765119
@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
 

Accepted Solution

by:
ztekin earned 0 total points
ID: 37765611
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37766340
@ingriT
Did not mean to. Did not notice that link.
0
 

Author Closing Comment

by:ztekin
ID: 37790270
There is open code and explanation.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now