Solved

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

Posted on 2012-03-23
16
1,040 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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…

809 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