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

Regards,
Leonard
<asp:UpdatePanel ID="updatePnlSave" runat="server">
            <ContentTemplate>
                <asp:Label ID="lblDebug" runat="server" Visible="true"></asp:Label>
                <asp:Label ID="lblAmountOfCompaniesFound" runat="server" Visible="False" style="margin-left:20px;"></asp:Label>
                <br />
                <asp:GridView ID="GridIdentifyAccount" runat="server" 
                    AutoGenerateColumns="False" CellPadding="3" CssClass="crmGrid" 
                    style="margin-top:10px">
                    <Columns>
                        <asp:TemplateField HeaderStyle-CssClass="borderFix">
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"
                                    AutoPostBack="false" ToolTip="Select/Deselect All" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelect" runat="server" />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="Bedrijfsnaam" HeaderText="Bedrijfsnaam" />
                        <asp:BoundField DataField="Codeleerbedrijf" HeaderText="Code leerbedrijf" />
                        <asp:TemplateField HeaderText="Status in CRM" ShowHeader="False">
                            <ItemTemplate>
                                <asp:Label ID="imageStatus" runat="server" />
                            </ItemTemplate>
                            <ItemStyle Font-Size="XX-Small" HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="CrmStatus" HeaderText="">
                            <ItemStyle HorizontalAlign="Left" ForeColor="White" />
                        </asp:BoundField>
                    </Columns>
                </asp:GridView>
                <asp:ObjectDataSource ID="leerbedrijvenSearchResult" runat="server"></asp:ObjectDataSource>
                <br />
                <asp:Label ID="lblSuccess" runat="server" Visible="False"></asp:Label>
                <br />
                <br />
                <asp:Button ID="btnImport" runat="server" Text="Records verwerken" CssClass="formButton"
                    Width="126px" OnClick="BtnImportClick" Enabled="False" />
                <br />
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Button ID="btnClose" runat="server" Text="Sluiten" CssClass="formButton" OnClick="BtnCloseClick" />



protected void BtnImportClick(object sender, EventArgs e)
    {
        lblSuccess.Text = "";

        bool success = false;
        int index = 0;

        // determine if nothing was processed
        bool nothingAtAll = true;
        lblSuccess.Text += "(rowcount=" + GridIdentifyAccount.Rows.Count + "), ";
        foreach (GridViewRow row in GridIdentifyAccount.Rows)
        {
            CheckBox chb = row.FindControl("chkSelect") as CheckBox;

            lblSuccess.Text += "checkbox=" + chb.Checked;
            if (chb != null && chb.Checked)
            {
                lblSuccess.Text += "!";

                var resultRow = SearchResultsDataTable[index];
                success = AddLeerbedrijvenToCrm(resultRow);
                nothingAtAll = false;
            }
            lblSuccess.Text += ", ";
            index++;
        }

        // refresh van grid
        var tijdelijkestring = lblSuccess.Text;
        SearchCodeLeerBedrijf(txtBedrijfsnaam.Text, txtPostcode.Text, int.Parse(txtHuisnummer.Text), txtBrinKenniscentrum.Text);

        lblSuccess.Text = success
                              ? "Record(s) succesvol bijgewerkt."
                              : "Er is een fout opgetreden bij het verwerken, probeert u het later nogmaals.";
        lblSuccess.Text = tijdelijkestring;

        if (nothingAtAll)
        {
            lblSuccess.Text += "Er is geen enkel record verwerkt (was er een selectie?)";
        }

        lblSuccess.Visible = true;
        btnImport.Enabled = true;
        lblAmountOfCompaniesFound.Visible = true;
    }

Open in new window

Leonard4pAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gene_CypCommented:
Well, are you sure your datagrid has data?
 
Rows = 0 it seems either your datasource is empty or no data came through.
0
Leonard4pAuthor Commented:
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)




        var a = GridIdentifyAccount.DataSource;
        if (a == null)
            lblSuccess.Text += "(DataSource==null)";
        else
            lblSuccess.Text += "(DataSource==" + a.ToString() + ")";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            GridIdentifyAccount.DataSource = SearchResultsDataTable;
            GridIdentifyAccount.DataBind();
        }
    }

Open in new window

0
Gene_CypCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Leonard4pAuthor Commented:
When I add a break, I see that SearchResultsDataTable is filled (Count=3 and lots of other data)

I also put this code in the Page_Load

            var a = GridIdentifyAccount.DataSource;
            lblSuccess.Text = "";
            if (a == null)
                lblSuccess.Text += "(Page_Load, DataSource==null)";
            else
                lblSuccess.Text += "(Page_Load, DataSource==" + a.ToString() + ")";

When I run from Visual Studio, i get (Page_Load, DataSource==SearchResult),   BUT
when I run the published website, I get (Page_Load, DataSource==null)






To your other question (Are you sure everything is properly loaded? Where does the SearchResultsDataTable get filled?). I think everything is properly loaded:

In the file SearchResultSet.xsd, I have a "SearchResult".

in the function ShowDataInGrid I fill the "SearchResultSet"

Then, I assign the DataSource to the grid (GridIdentifyAccount.DataSource = SearchResultsDataTable;)



    private void ShowDataInGrid(IdentificatieResponse response)
    {
        using (var data = new SearchResultSet.SearchResultDataTable())
        {
            lblAmountOfCompaniesFound.Text = string.Empty;
            foreach (IdentificatieBedrijf bedrijf in response.Bedrijven)
            {
                var row = data.NewSearchResultRow();

                row.Bedrijfsnaam = bedrijf.Bedrijfsnaam;
                row.Codeleerbedrijf = bedrijf.Codeleerbedrijf;
                data.Rows.Add(row);
            }

            //data aan SearchResultsDatatable toevoegen
            if (ViewState["SearchResultsDatatable"] == null)
            {
                ViewState.Add("SearchResultsDatatable", data);
            }
            else
            {
                ViewState["SearchResultsDatatable"] = data;
            }

            //Bind de DataSet aan de GridView
            GridIdentifyAccount.DataSource = SearchResultsDataTable;
            GridIdentifyAccount.DataBind();
            GridIdentifyAccount.Visible = true;
        }
    }

Open in new window

0
Leonard4pAuthor Commented:
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!)


    protected void BtnImportClick(object sender, EventArgs e)
    {

        var a = GridIdentifyAccount.DataSource;

        bool success = false;
        int index = 0;

        // determine if nothing was processed
        bool nothingAtAll = true;
        lblSuccess.Text = "";
        lblSuccess.Text += "(rowcount=" + GridIdentifyAccount.Rows.Count + "), ";
        foreach (GridViewRow row in GridIdentifyAccount.Rows)
        {
            //CheckBox chb = row.FindControl("chkSelect") as CheckBox;
            CheckBox chb = (CheckBox)row.FindControl("chkSelect");

            lblSuccess.Text += "checkbox=" + chb.Checked;
            if (chb != null && chb.Checked)
            {
                lblSuccess.Text += "!";

                var resultRow = SearchResultsDataTable[index];
                success = AddLeerbedrijvenToCrm(resultRow);
                nothingAtAll = false;
            }
            lblSuccess.Text += ", ";
            index++;
        }
        if (nothingAtAll)
        {
            lblSuccess.Text += "Er is geen enkel record verwerkt (was er een selectie?)";
        }

        if (a == null)
            lblSuccess.Text += "(BtnImportClick, DataSource==null)";
        else
            lblSuccess.Text += "(BtnImportClick, DataSource==" + a.ToString() + ")";

        lblSuccess.Visible = true;
        btnImport.Enabled = true;
        lblAmountOfCompaniesFound.Visible = true;
    }

Open in new window

0
Gene_CypCommented:
The devil is always in the detail ;)
 
For the checkbox, add a handler  "OnCheck" and see if it gets fired (again using a breakpoint)
0
Gene_CypCommented:
The handler of course to fire for the "CheckedChanged" event
0
Leonard4pAuthor Commented:
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.



    public void GridView1_CheckedChanged(Object sender, EventArgs e)
    {
        lblDebug.Text += "!!!!";
		var chbox = sender as CheckBox;

		if (chbox == null) return;

		var gridViewRow = chbox.Parent.Parent as GridViewRow;
		if (gridViewRow == null) return;

		if (chbox.Checked)
		{
			gridViewRow.BackColor = GridView1.SelectedRowStyle.BackColor;
			gridViewRow.ForeColor = GridView1.SelectedRowStyle.ForeColor;
		}
		else
		{
			gridViewRow.BackColor = GridView1.RowStyle.BackColor;
			gridViewRow.ForeColor = GridView1.RowStyle.ForeColor;
		}

		var row = SearchResultsDataTable.Rows[gridViewRow.DataItemIndex] as ColoSearchResultSet.SearchResultRow;
		lblDebug.Text += "CheckedChanged:" + chbox.Checked + ".";
		if (row != null) row.Geselecteerd = chbox.Checked;
    }




    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var published = true;
#if DEBUG
            published = false;
#endif
            if (published)
            {
                //reBind de DataSet aan de GridView
                GridView1.DataSource = SearchResultsDataTable;
                GridView1.DataBind();
            }
        }
    }

Open in new window

0
Gene_CypCommented:
Try setting this:
AutoPostBack="false"
to true
0
Leonard4pAuthor Commented:
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.
0
Gene_CypCommented:
That's because given the postback code that I see above, you only have it binding if there is no postback.
 
0
Gene_CypCommented:
Also, try subscribing to the CheckChanged event in your Page_Init instead.
0
Gene_CypCommented:
Check that the Enable property is NOT set to false in the page_load
0
Leonard4pAuthor Commented:
problem is fixed
0
Leonard4pAuthor Commented:
Thanks Gene_Cyp, problem is fixed!


aspx:

                                                            <asp:CheckBox ID="chkSelect" runat="server" />


code behind:

        if (Page.IsPostBack)
        {
            var published = true;
#if DEBUG
            published = false;
#endif
            if (published)
            {
                //reBind de DataSet aan de GridView
                GridView1.DataSource = SearchResultsDataTable;
                GridView1.DataBind();
            }
        }



    protected void BtnSaveClick(object sender, EventArgs e)
    {
        bool success = false;
        int index = 0;
        // determine if nothing was processed
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chb = row.FindControl("chkSelect") as CheckBox;

            if (chb != null && chb.Checked)
            {
                var resultRow = SearchResultsDataTable[index];
                success = Processor.CreateInternship(resultRow);
            }
            index++;
        }
    }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.