Gridview empty when SelectedIndexChanged called

Posted by xan on Stack Overflow See other posts from Stack Overflow or by xan
Published on 2010-04-08T10:32:40Z Indexed on 2010/04/08 11:23 UTC
Read the original article Hit count: 483

Filed under:
|
|
|
|

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 GridVeiw 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!

© Stack Overflow or respective owner

Related posts about gridview

Related posts about c#