处理PageIndexChanging事件

时间:2013-03-11 11:59:40

标签: c# asp.net linq page-index-changed

我有一个在用户提交搜索并且加载了gridview时运行的查询。

我想寻找gridview,并且通常会在LoadGrid()中放置我的网格。我的分页工作正常,一切都很好,直到我想查看GridView的第2页。

protected void btn_Search_Click(object sender, EventArgs e)
{
// Init()
// -----
pnl_Message.Visible = false;
lbl_message.Text = String.Empty;

// Clear grid of previous results
// ==============================
ResultsGridView.DataSource = null;
ResultsGridView.DataBind();

try
{
    using (var db = new dbDataContext())
    {
        // Check Fields
        // ------------
        if (txt_CustomerName.Text == string.Empty && txt_CCNo.Text == string.Empty &&
            txt_SwiftNo.Text == string.Empty && drp_Provider.SelectedValue == string.Empty)
        {
            lbl_message.Text += "* Please enter a search term";
            pnl_Message.Visible = true;
            pnl_Message.CssClass = "loginError";
            pnl_results.Visible = false;
        }

        // Search Database
        // ---------------
        if (lbl_message.Text == String.Empty)
        {
            var customer = txt_CustomerName.Text.Length > 1 ? txt_CustomerName.Text.Trim() :
            "Xcxcx";
            //var provider = drp_Provider.SelectedItem.Text.Trim();
            var concern = txt_CCNo.Text == "" ? 0 : Convert.ToInt32(txt_CCNo.Text);
            var swiftid = txt_SwiftNo.Text == "" ? 0 : Convert.ToInt32(txt_SwiftNo.Text);

            // Check which fields populated
            // ----------------------------
            var result =
                from c in db.tbl_Concerns
                where (c.ProviderId == Convert.ToInt32(drp_Provider.SelectedValue))
                    || (c.person_Fullname.Contains(customer))
                    || (c.SwiftId == swiftid)
                    || (c.ConcernId == concern)
                select new
                {
                    c.ConcernId,
                    Swift = c.SwiftId,
                    FullName = c.person_Fullname,
                    Provider = c.tbl_Provider.provider_Name,
                    Concern_Detail = c.tbl_RaisedConcern.RaisedConcernText,
                    DateFrom = c.concern_DateFrom,
                    DateTo = c.concern_DateTo
                };
            // If No Results
            // -------------
            if (!result.Any())
            {
                lbl_message.Text += "* No results match your search, please try again.";
                pnl_Message.Visible = true;
                pnl_Message.CssClass = "loginError";
            }
            else
            {
                // Show Table
                // ----------
                pnl_results.Visible = true;
                ResultsGridView.DataSource = result;
                ResultsGridView.DataBind();

                // Table Header Names
                // ------------------
                ResultsGridView.HeaderRow.Cells[0].Text = "Select";
            }
        }
    }
}
catch (SystemException ex)
{
    var exceptionUtility = new genericExceptions();
    exceptionUtility.genericSystemException(
        ex,
        Server.MachineName,
        Page.TemplateSourceDirectory.Remove(0, 1) + Page.AppRelativeVirtualPath.Remove(0, 1),
        ConfigurationManager.AppSettings["emailSupport"],
        ConfigurationManager.AppSettings["emailFrom"],
        string.Empty);
}
}

我的PageIndexChanging事件我通常称之为loadGrid(),但我没有这种情况。我尝试添加ResultsGridView.DataBind(),但收到错误Sequence contains no elements

protected void ResultsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
  ResultsGridView.PageIndex = e.NewPageIndex;
  ** How Can I bind the Grid**
}

1 个答案:

答案 0 :(得分:2)

试试这个..

session["somename"]=result;

protected void ResultsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {
   var somename=session["somename"].Tostring();  
  ResultsGridView.PageIndex = e.NewPageIndex;
  ResultsGridview.Datasource=somename;
  REsultsGridview.Databind();

 }