We help IT Professionals succeed at work.

How do I repopulate and rebind a Repeater with a different data source after a Postback?

1,405 Views
Last Modified: 2013-11-07
I have a repeater that initallly retrieve values from a database and the data source is based on the relevant fields names of the query created.  The fields in the repeater are populated with one or more fields from the datbase as shown on the ItemDataBound event

                Dim t As New TextBox
                t = e.Item.FindControl("EmployeeID")
                t.text = e.Item.DataItem("ID")
                t = e.Item.FindControl("EmployeeName")
                t.text = e.Item.DataItem("Surname") + " " + e.Item.DataItem("FirstName")

Open in new window


On a postback, I want to update the repeater, without accessing the database and utilising the existing data in the repeater.  I have created a DataTable, one for each of the controls in the repeater which then loops through each of the rows and rebinds to the repeater.

       Dim aTable As New System.Data.DataTable
        With aTable
            .Columns.Add("EmployeeID")
            .Columns.Add("EmployeeName")
        End With

Open in new window


The issue comes on the ItemDataBound event, when the repeater is rebound, where the data source is now different as the field names to the data source are now different.

The only way I believe to solve this issue to determine the data source that populates the repeater and to handle the data source approriately within the ItemDataBound event...

How do I identifiy the data source so I can handle the temDataBound correctly so the data source is repopulated using the datatable?
Comment
Watch Question

Kaushal AroraTechnical Analyst

Commented:
You can get the same DataSource with which you have bound the repeater control using the syntax:

Dim objData as object = Repeater1.DataSource
' The Repeater1.DataSource property: Gets or sets the data source that provides data for populating the list.

So now as you have got the same DataTable with which you have bound the Repeater first you can bind it again with the same code and with no changes to ItemDataBound Event Code.

Hope It Helps You.

Author

Commented:
Hi KaushalArora,

Unfortunately this does not work....  When updated, the repeater section returns a blank space.

The code to the AddEmployee button is attached. I have put a message box to test the number of items in the data source, however it appears to be zero.

The purpose of the two data sources was to add a new employee item to the data source which I then save as a group when the user presses the Save button.


Public Sub AddEmployee(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewEmployee.Click
        Dim objData As Object = Repeater1.DataSource
        Repeater1.DataSource = objData
        Repeater1.DataBind()
        MsgBox(Repeater1.Items.Count.ToString)
End Sub

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.