Solved

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

Posted on 2012-03-23
16
1,032 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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
 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 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