Solved

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

Posted on 2012-03-23
16
1,066 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

729 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