Using List<T> causes second Page Load in ASP.Net
I have a fairly simple web application that gets a list of items from a database (in a DataTable), and binds a view of that DataTable to a Repeater.
When converting my DataTable to a List (which is done in a class library), Page Load fires a second time! Walking though the debugger, the same items are in the list that were in the DataTable.
The only code in my page was:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptOffers.DataSource = DataAccess.GetOfferList(offerId); // returns List<T>
rptOffers.DataBind();
}
}
public static List<OfferItem> GetOfferList(int offerId)
{
DataTable dtOffers = GetOfferData(offerId);
List<OfferItem> offers = new List<OfferItem>();
// loop throw all of the offers
foreach (DataRow dr in dtOffers.Rows)
{
// add each offer to the List<>
OfferItem currentOffer = new OfferItem();
// initialize the OfferItem properties...
offers.Add(currentOffer);
}
return offers;
}
When I chang开发者_如何学运维e it back to this, it works fine:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptOffers.DataSource = DataAccess.GetOfferItems(offerId);
rptOffers.DataBind();
}
}
Is there anything else I need to do in my List to keep it from running Page Load again?
I have experienced this issue when AutoEventWireup is set to true in my .aspx file
<%@ ... AutoEventWireup="True" ... %>
and my InitializeComponent function still contains a hookup for Page_Load
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
精彩评论