GridView with SqlDataSource - Sorting /Paging

Last post 11-08-2009 1:57 AM by PeteNet. 3 replies.

Sort Posts:

  • GridView with SqlDataSource - Sorting /Paging

    11-07-2009, 6:57 PM
    • Member
      60 point Member
    • scptech
    • Member since 10-11-2005, 8:27 PM
    • Posts 182

    Using ASP.NET 3.5 / VS 2008 / C#.

    I decided to finally try to use the SqlDataSource - after hearing about how it handles paging / sorting automatically (albeit with some efficiency issues).

    The GridView displays initially, then no paging or sorting happens. Just a page postback.

    I assign the connectionstring and SelectCommand in a Button click event.

    If the GridView supposed to be DataBound on each Page Load when using a SqlDataSource? I have tried all sorts of combnations with no luck.

     

  • Re: GridView with SqlDataSource - Sorting /Paging

    11-07-2009, 8:08 PM
    • All-Star
      26,884 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,834

    scptech:

    I assign the connectionstring and SelectCommand in a Button click event.

    If the GridView supposed to be DataBound on each Page Load when using a SqlDataSource?

    IF you do specify the connection and SelectCommand declaratively, you won't need any additional code on each postback (page load)...all you'd do is specify the AllowPaging and AllowSorting properties on the GridView.

    check out the links for some examples.

    Regards,
    Peter
  • Re: GridView with SqlDataSource - Sorting /Paging

    11-07-2009, 11:52 PM
    • Member
      60 point Member
    • scptech
    • Member since 10-11-2005, 8:27 PM
    • Posts 182

    Thank you for responding.

    I do not assign connection or SelectCommand declaratively. 

  • Re: GridView with SqlDataSource - Sorting /Paging

    11-08-2009, 1:57 AM
    Answer
    • All-Star
      26,884 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,834

    it (SqlDataSource) doesn't store the SelectCommand in the ViewState. you'll need to set up a ViewState variable in the button click and restore it to the SDS on page load.

            protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["Select"] != null)
                {
                    SqlDataSource1.ConnectionString = @"your connection string here";
                    SqlDataSource1.ProviderName = "System.Data.SqlClient";
                    SqlDataSource1.SelectCommand = ViewState["Select"].ToString();
                    GridView1.DataSourceID = "SqlDataSource1";
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                string sqlSelect = "SELECT statement here";
                ViewState["Select"] = sqlSelect;
                SqlDataSource1.ConnectionString = @"your connection string here";
                SqlDataSource1.ProviderName = "System.Data.SqlClient";
                SqlDataSource1.SelectCommand = sqlSelect;
                GridView1.DataSourceID = "SqlDataSource1";
                GridView1.DataBind();
            }

    VB.NET ? convert here: http://www.developerfusion.com/tools/convert/csharp-to-vb/





    Regards,
    Peter
Page 1 of 1 (4 items)