Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Displaying Data using Repeater Control

Status
Not open for further replies.

cyberstore05

Programmer
Jan 4, 2008
2
GB
Hi There,
I am trying to display data from an SQL database using the Repeater control in ASP.net, but I’ve run into some difficulty. Primarily, when a user selects an item from a menu, data from an SQL table is displayed using Gridview – this works perfectly ok. Additionally, I want a subset of the Gridview data to be displayed using the Repeater control - this is where I have the problem: the two sets of data are not displayed simultaneously when a menu selection is made. Instead, the data from the previous menu selection is displayed. In short, the Repeater data lags behind the Gridview data.

Sub Page_Load
If Not Page.IsPostBack then
Dim SQLstring As Object
If MenuSearch = "selected item" Then
SQLString = "SELECT * FROM ProductsTab " & _
"WHERE OuterCategory = '" & MenuSearch & "' " & _
"IF OBJECT_ID(N'ProductsDB..BrandsTab') IS NOT NULL " &
"DROP TABLE BrandsTab " & _
"SELECT Brand, COUNT(*) AS BrandCount INTO BrandsTab FROM ProductsTab " & _
"WHERE OuterCategory = '" & MenuSearch & "' " & _
"GROUP BY Brand "
……………
SqlDataSource2.SelectCommand = SQLString
End If
End Sub
Note: Data from ProductsTab is displayed in Gridview, data from BrandsTab is displayed in Repeater. The subset of selection from ProductsTab is saved in BrandsTab.
The Repeater code is:-
<asp:Repeater runat="server" id="Repeater1" DataSourceID="SqlDataSourceBrand" OnItemCreated="Repeater1_ItemCreated" >
<ItemTemplate>
<asp:Label Text='<%# Eval("Brand") %>' runat="server"/>
<br />
<asp:Label Text='<%# Eval("BrandCount") %>' runat="server"/>
</ItemTemplate>
</asp:Repeater>
I hope the above code helps to explain my problem. I would greatly appreciate any help.
 
I would seriously reconsider your overall design. You are following some less than desirable practices. However, if you must make what you are doing work, here is what you can do. First, I would recommend doing this in a separate class entirely, you can call it "DataAccess," or something to that effect. Now create a method that retrieves the same recordset that your code in page_load is retreiving. Make this method return the recordset in the form of a datatable (or any other bindable source). Then call that method from you page load and bind the datatable to your controls... like so...

Code:
Sub Page_Load 
  If Not page.IsPostBack Then
    Dim dt as datatable = DataAccess.GetData()
    myGridview.DataSource = dt
    myGridview.DataBind()
    myRepeater.DataSource = dt
    myRepeater.DataBind()
  End If
End Sub

Hope this helps.

Kevin Davie
Consultant
Sogeti USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top