Gridview empty when SelectedIndexChanged called

2019-08-31 13:20发布

问题:

I have a DataGrid which is being bound dynamically to a database query. The user enters some search text into a text field, clicks search, and the code behind creates the appropriate database query using LINQ (searches a table based on the string and returns a limited set of the columns).

It then sets the GridView datasource to be the query and calls DataBind().

protected void btnSearch_Click(object sender, EventArgs e)
{
    var query = from record in DB.Table
                where record.Name.Contains(txtSearch.Text) //Extra string checking etc. removed.
                select new
                {
                    record.ID,
                    record.Name,
                    record.Date
                };

    gvResults.DataSource = query;
    gvResults.DataBind();
}

This works fine.

When a user selects a row in the grid, the SelectedIndexChanged event handler gets the id from the row in the grid (one of the fields), queries the full record from the DB and then populates a set of editor / details fields with the records full details.

protected void gvResults_SelectedIndexChanged(object sender, EventArgs e)
{
    int id = int.Parse(gvResults.SelectedRow.Cells[1].Text);
    DisplayDetails(id);
}

This works fine on my local machine where I'm developing the code. On the production server however, the function is called successfully, but the row and column count on gvResults, the griview is 0 - the table is empty.

The gridview's viewstate is enabled and I can't see obvious differences. Have I made some naive assumptions, or am I relying on something that is likely to be configured differently in debug?

Locally I am running an empty asp.net web project in VS2008 to make development quicker. The production server is running the sitecore CMS so is configured rather differently.

Any thoughts or suggestions would be most welcome. Thanks in advance!

回答1:

Check your web.config. Likely the "AutomaticDataBind" property is set to "false" in one environment, and "true" on your dev box.

I cannot be sure, obviously, but I've been hammered by a similar issue in the past and the symptoms were exactly like you describe here :-)

P.S. Sitecore defaults this value to false.



回答2:

Having poked around the sitecore forums some more, I came across this blog post explaining one potential solution.

I added <type>System.Web.UI.WebControls.GridView</type> to the <typesThatShouldNotBeExpanded> section of Web.config and it seems to work for us.

It seems to be to do with sitecore's page layout rendering pipeline, where it expands sub-layouts and internal placeholders to generate the full page rendering. It accesses the .Net controls on the page and pokes them around a bit, which can cause some controls to not work correctly.

There is an article on the SDN about this, although you can only read it if you have an account with sufficient privalegs. Hope this might help any other sitecore users out there in future.