1
votes

I have a gridView which initializes the page using the full contents of a table using source DataSourceID="SqlDataSource1"

testPage.aspx:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ 
ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT [Field1], [Field2], [Field3], [Field4] FROM [Analysis]
ORDER BY [Field1] DESC">
</asp:SqlDataSource>

(I've changed the real field names!).

This works fine and the gridView populates with the full table data. Based on another event I now want to change the SelectCommand to filter the table using the SQL WHERE clause. I've tried

testPage.aspx.cs:

    protected void ctl04_ValueChanged(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT [Field1], [Field2], 
        [Field3], [Field4] FROM [Analysis] WHERE [Field1] = 'items' ORDER BY 
        [Field1] DESC";
        gridView1.DataBind();
    }

The event fires and appears to run fine but gridView1 doesn't refresh/update and still contains the data for the full table.

Can anyone give some pointers how to get this working? Thanks

2
Try GridView.DataSource = null; before you reassign the select command. This should force the GridView to rebind using the new SelectCommand. - Evan L
That's not working for me, it runs okay but gridView1 still contains the full table data after the event fires. - user857521

2 Answers

2
votes

I think the problem is PostBack and page load. When your page opened and Page_load method was called. then the GridView refreshed using default SELECT Statement. You should put refresh code into this code:

if (!Page.IsPostback)
{
   // Your Code
}
1
votes

how about Create 2nd sql data source and you can switch between them. I think that's happen because of post back