简体   繁体   中英

call function with parameters in the Page Load .NET

I'm doing a filter form by 3 criteria, I'm having problems on the pagination since I do not load the data from the second page of the GridView, apparently I have to call my function that fills the GridView in the Load page event, but this function receives parameters that are the ones that the user enters in the search filter, How can I then call the function sending the parameters to that function in the page load event every time the page is changed in the GridView?

function

        public DataTable AdvertSearch(string tittle, DateTime datel, DateTime date2)
{
    SqlConnection con = new SqlConnection(Util.GetConnectionString("ConnectionString"));
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select*from table where titAdvert=@tittle or dateStart=@dateS" or dateEnd=@dateE;
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@tittle", tittle== "" ? (object)DBNull.Value : tittle);
    cmd.Parameters.AddWithValue("@dateS", date1== "" ? (object)DBNull.Value : date1);
    cmd.Parameters.AddWithValue("@dateE", date2== "" ? (object)DBNull.Value : date2);
    cmd.Connection = con;
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

search button

    protected void BtnSearch_Click(object sender, EventArgs e)
{
        GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
        GridView1.DataBind();

}

You need to specify a pageIndexChanging event to trigger the grid to be reloaded when the user clicks to the next page. Check out the code below:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{  
    GridView1.PageIndex = e.NewPageIndex;  
    BindData();  
}

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
    GridView1.DataBind();
}

And please write your code in english, as it makes it more readable for all programmers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM