code behind does not recognize GridView (?!)

Hi,

I have a form with a button and a gridview. When I run my aspx from Visual Studio 2010 and when I click the button, the GridIdentifyAccount.Rows.Count in the code behind returns the number of rows of the gridview. So far, so good.

However, when I publish the Web Site and run the apsx and hit the button, the GridIdentifyAccount.Rows.Count returns 0. It seems that the code cannot find the gridview(?)

Any ideas?

Below you can find the aspx-code for the gridview and button and the code behind for the button_click

Rows = 0 it seems either your datasource is empty or no data came through.

0

Leonard4pAuthor Commented: 2010-04-07

The gridview has data (on the page I see 3 rows). But, when I run the page from Visual Studio, the GridIdentifyAccount.DataSource is filled. When I publish the Website and when I run the published site, then GridIdentifyAccount.DataSource == null

in the Page_Load, the datasource is binded to the gridview (see code below)

Add a break:
GridIdentifyAccount.DataSource = SearchResultsDataTable;
and tell me what you see.

Are you sure everything is properly loaded? Where does the SearchResultsDataTable get filled?
By the way, usually
1) you create a "DataSource" and
2) then add DataTables to it.
3) Populate the Table
4) You then assign that DataSource as a DataSource for your DataGridView

Ouch, the DataSource was null, because I forgot EnableViewState="true".
With EnableViewState="true", the DataSource is filled!

There is still one problem however: chb.Checked is always false, even if it's checked!

This only happens when I run the page from Visual Studio! When I run the published website, chb.Checked is true when it is selected. (very strange, that the published program reacts different in Visual Studio!)

It's very weird: the OnCheckChanged event handler for the checkboxes is fired in debug mode (when I run the program in Visual Studio). The same code is not fired when I run the published version (?!?!?!?!)

Here's the code for the checkbox in the gridview
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="GridView1_CheckedChanged" AutoPostBack="false" />

The code for the OnCheckChanged is below. I also placed the code in the Page_Load (the strage code in the "Not IsPostback" part is because the published version does not reminder the SearchResultsDataTabl, so I have to bind again.

With AutoPostBack="true", the OnCheckChanged event handler is fired in Visual Studio (the OnCheckChanged event handler is also fired with AutoPostBack="false").

In the published version, with AutoPostBack="true", the grid disappears when I select a checkbox. Probably because the click on the checkbox generates a postback, which hit the Page_Load, which binds the SearchResultsDataTable to the gridview. The SearchResultsDataTable is empty in the published version (??!??), while in Visual Studio, the SearchResultsDataTable is filled.