Select method of ObjectDataSource called twice - second post

  • Thread starter Thread starter TarTar
  • Start date Start date
T

TarTar

Hello,

I have already posted this problem, but I have not received any response
yet. I will try to describe it again.

We have a list control (e.g. DataList) and an ObjectDataSource on an ASP.NET
web page. When we open the page the Select method (here: GetCustomers) is
called twice. Why? It is obvious performance hit as the data needs to be
retrieved twice. How to avoid the duplicated calls?

Below there is complete code for both the ASP.NET page and its code-behind:

<asp:DataList ID="CustomersList" DataSourceID="CustomerDataSource"
runat="server">
<ItemTemplate>
Name: <asp:Label id="lblName" Text='<%# Eval("Name") %>'
runat="server"/>
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="CustomerDataSource" SelectMethod="GetCustomers"
TypeName="Customers" runat="server" />

public class Customers
{
public List<Customer> GetCustomers()
{
Customer test = new Customer();
test.Name = "Uakari Mabuto";
List<Customer> list = new List<Customer>();
list.Add(test);
return list;
}
}
public class Customer
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}


Thanks,
Leszek
 
It does not look like you posted the actual code behind for the page here,
or is that an empty file? I see nothing wrong with the class file or the
ASP.NET tagged portion.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
It sounds like the databinding is being called twice. Check your
Page_Load event to see if you're doing something like calling
Page.DataBind(), then DataList1.DataBind().

You also could have something in the Page_Load that causes it to re-load
the current page??


Steve C.
MCAD,MCSE,MCP+I,CNE,CNA,CCNA
 
Thanks Steve and Cowboy for the answers.

I agree - the ASP.NET code and C# code looks fine. There is also no code in
Page_Load. Yet, the GetCustomers() method is called twice when the page is
called first time. It looks like a built-in feature of ObjectDataSource (or
the DataList control?).
Whay is that? The code is very rudimentary - no parameters, no code in
Page_Load or any other event handler.

Thanks,
Leszek
 
Problem solved - but it is the weirdest quirk of ASP.NET (or Visual Studio
and its built-in web server) I have met so far.
It turned out the cause was not the ObjectDataSource or any event handler
but the filename. When I renamed the file from Default.aspx to
Homepage.aspx, calls are not duplicated any more.

Leszek
 
Back
Top