Triggered an unhandled PageIndexChanging event

When using GridView to implement paging and sorting

1. "Unprocessed PageIndexChanging event" will appear when you click on the label of the next page. At this time, you only need to add the OnPageIndexChanging event to the GridView label.

And make OnPageIndexChanging="GridView2_PageIndexChanging"

Then add the following function in the background code:

protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
DataBind();
}

2. When you click on the title bar to achieve sorting, an "unprocessed Sorting event has been triggered" appears. At this time, add the OnSorting event to the GridView tag and use

OnSorting=" GridView2_Sorting"

The solution to this problem is as follows:

My development environment: There is a HouseDB.cs file with a GetHouseInfo function and a front page HaveShiSearchResult.

A function is created in the HouseDB.cs file, and the code function is as follows:

   public DataSet GetHouseInfo(string Province, int Houseprice1, int Houseprice2)
    {

        SqlConnection myConnection = new SqlConnection(sqlString.Connection());
        SqlCommand myCommand = new SqlCommand("SearchHouse", myConnection);

      
        myCommand.CommandType = CommandType.StoredProcedure;

       

        SqlParameter parameterProvince = new SqlParameter("@Province", SqlDbType.NChar, 10);
        parameterProvince.Value = Province;
        myCommand.Parameters.Add(parameterProvince);
       
        SqlParameter parameterHouseprice1 = new SqlParameter("@Houseprice1", SqlDbType.Int);
        parameterHouseprice1.Value = Houseprice1;
        myCommand.Parameters.Add(parameterHouseprice1);

        SqlParameter parameterHouseprice2 = new SqlParameter("@Houseprice2", SqlDbType.Int);
        parameterHouseprice2.Value = Houseprice2;
        myCommand.Parameters.Add(parameterHouseprice2);
       
        myConnection.Open();

         DataSet dtr = new DataSet();

        SqlDataAdapter result = new SqlDataAdapter(myCommand);

         result.Fill(dtr);

         return dtr; 

  }

HaveShiSearchResult.aspx.cs code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

            BindGrid();

        }    

   }

protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState[“sortexpression”] = e.SortExpression;
if (ViewState[“sortdirection”] == null)
{
ViewState[“sortdirection”] = “asc”;
}
else
{
if (ViewState[“sortdirection”].ToString() == “asc”)
{
ViewState[“sortdirection”] = “desc”;
}
else
{
ViewState[“sortdirection”] = “asc”;
}
}
BindGrid();
}

protected void BindGrid()

    {
        HouseManger.GetParmar showparam = new GetParmar();

        HouseManger.HouseDB SearchHouseInfo = new HouseManger.HouseDB();

        DataSet ds = new DataSet();

         ds= SearchHouseInfo.GetHouseInfo(showparam.getProvince(),showparam.getHouseprice1(), showparam.getHouseprice2());    

        DataView dv = ds.Tables[0].DefaultView;

        if (ViewState["sortexpression"] != null)
        {
            dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
        }

        GridView2.DataSource = dv;

        GridView2.DataBind();         

    }    

Guess you like

Origin blog.csdn.net/qq_41661800/article/details/105714499