簡體   English   中英

頁面加載.NET中帶有參數的調用函數

[英]call function with parameters in the Page Load .NET

我正在按照3個條件來做一個過濾器表單,在分頁時遇到了問題,因為我沒有從GridView的第二頁加載數據,顯然我必須在Load page事件中調用填充GridView的函數,但是此函數接收的參數是用戶在搜索過濾器中輸入的參數,那么每次在GridView中更改頁面時,如何調用該函數在頁面加載事件中將參數發送給該函數?

功能

        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;
}

搜索按鈕

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

}

您需要指定一個pageIndexChanging事件,以在用戶單擊下一頁時觸發重新加載網格。 查看以下代碼:

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();
}

並且請用英語編寫您的代碼,因為它使所有程序員都更容易理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM