使用列表导致 ASP.Net 中的第二个页面加载
我有一个相当简单的 Web 应用程序,它从数据库(在 DataTable 中)获取项目列表,并将该 DataTable 的视图绑定到 Repeater。
当将我的 DataTable 转换为列表(在类库中完成)时,页面加载会第二次触发!通过调试器,列表中的项目与数据表中的项目相同。
我的页面中唯一的代码是:
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;
}
当我将其更改回此时,它工作正常:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rptOffers.DataSource = DataAccess.GetOfferItems(offerId);
rptOffers.DataBind();
}
}
我的列表中还需要做其他事情以防止它再次运行页面加载吗?
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 change 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我的 .aspx 文件中 AutoEventWireup 设置为 true
并且我的 InitializeComponent 函数仍然包含 Page_Load 的连接时,我遇到了这个问题
I have experienced this issue when AutoEventWireup is set to true in my .aspx file
and my InitializeComponent function still contains a hookup for Page_Load