Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm checking for the number of items in a control (System.Web.UI.Controls.FormView.DataItemCount) within Page_Load, and, somehow the value is being returned as '3' even when the FormView has not yet been populated.

I'm following this MSDN article, and, currently using VS 2008 / Win XP SP 3 / IIS 5.1 on my workhorse...laptop used for development.

Any idea why?
:confused:
C#
<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
   {
       this.Title = "Jon's Couch";
       Envelope.DataBind();
       //select last date selected on calArticles
       if (this.ViewState["CalendarDate"] != null)
       { calArticles.SelectedDate = (DateTime)this.ViewState["CalendarDate"]; }
       if (Envelope.DataItemCount > 0)
       {
           btnEdit.Enabled = true;
           btnSave.Enabled = false;
           btnCancel.Enabled = false;
           btnRefresh.Enabled = true;
           btnDelete.Enabled = true;
       }
       else if (Envelope.DataItemCount == 0) 
//TODO: Bug - Envelope.DataItemCount is '3' instead of '0' when there are no data items.
       {
           btnEdit.Enabled = false;
           btnSave.Enabled = false;
           btnCancel.Enabled = false;
           btnRefresh.Enabled = true;
           btnDelete.Enabled = false;
       }
   }


Complete source code here.
Posted

1 solution

Whats the point of having Envelope.DataBind(); in second line of your page load?

Probably, your databind is storing the data in ViewState and thus you get 3 counts all the time. Should you be using a IsPostback page proerty?
 
Share this answer
 
Comments
jon-80 2-Sep-10 6:29am    
Thanks.

IsPostback was not used, because when I was planning the logic, I wasn't bothered whether the Page was being loaded for the first time or as a postback (refresh).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900