在按钮单击事件上绑定GridView与分页

时间:2014-04-14 03:10:29

标签: c# asp.net gridview binding event-handling

我是asp.net的新手,需要一些帮助。每页每20条记录有一个gridview with paging,我在gridview外面有一个搜索按钮。我需要做的是当我单击搜索按钮时,结果必须绑定到gridview(现在正在发生),但是当记录超过pagesize并且我需要转到网格的下一页时,绑定丢失,绑定记录是在加载事件上形成页面的记录。下面是我的代码示例。

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}

public void BindData()
{

    {
        List<EventFile> eventFile = new List<EventFile>();
        eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U");
        if (gvwAssociation.DataSource == null)
        {
            gvwAssociation.DataSource = eventFile;
            gvwAssociation.DataBind();
        }
    }
}
 protected void btnSearch_Click(object sender, EventArgs e)
{
    int uFlag = 0;
    string uploadFlag = this.ddlUploadDate.SelectedValue;
    string fileName = this.txtSearchText.Text;
    string uploadDt = this.txtDate.Text;
    string status = this.ddlStatus.SelectedValue.ToString();
    bt = true;


    if (status == "Un-Assigned")
    {
        status = "U";
    }
    else if (status == "Assigned")
    {
        status = "A";
    }
    else
    {
        status = "B";
    }


    if ((uploadFlag == "On") && (uploadDt == ""))
    {
        uFlag = 0;
    }
    else if (uploadFlag == "On")
    {
        uFlag = 1;
    }
    else if (uploadFlag == "OnorBefore")
    {
        uFlag = 2;
    }
    else
    {
        uFlag = 3;
    }


    fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);

    gvwAssociation.DataSource = fileSearch;
    gvwAssociation.DataBind();
}

 protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //SaveSelectedValues();
    gvwAssociation.PageIndex = e.NewPageIndex;
    //BindData();
    //PopulateSelectedValues();
}

1 个答案:

答案 0 :(得分:2)

首先,你应该有以下的分页事件处理程序

protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvwAssociation.PageIndex = e.NewPageIndex;
    bindGridWithFilter();
}

然后,将搜索按钮中的搜索/过滤逻辑移动到私有方法(例如&#34; bindGridWithFilter&#34;)

提示:尝试将BindData和bindGridWithFilter结合使用,这样当没有过滤器时,您会显示所有记录


<强>更新

这里有一些重构的代码,让您了解我的意思。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGridWithFilter();
            }
        }

        protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvwAssociation.PageIndex = e.NewPageIndex;
            bindGridWithFilter();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bindGridWithFilter();

        }

        private void bindGridWithFilter()
        {
            List<EventFile> eventFile = new List<EventFile>();
            eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U");
            if (gvwAssociation.DataSource == null) 
            {
                // If you don't have a filter you show all records
                gvwAssociation.DataSource = eventFile;
                gvwAssociation.DataBind();
            }
            else
            {
                // This is same as the logic in your search button
                // display only the filtered records
                int uFlag = 0;
                string uploadFlag = this.ddlUploadDate.SelectedValue;
                string fileName = this.txtSearchText.Text;
                string uploadDt = this.txtDate.Text;
                string status = this.ddlStatus.SelectedValue.ToString();
                bt = true;


                if (status == "Un-Assigned")
                {
                    status = "U";
                }
                else if (status == "Assigned")
                {
                    status = "A";
                }
                else
                {
                    status = "B";
                }


                if ((uploadFlag == "On") && (uploadDt == ""))
                {
                    uFlag = 0;
                }
                else if (uploadFlag == "On")
                {
                    uFlag = 1;
                }
                else if (uploadFlag == "OnorBefore")
                {
                    uFlag = 2;
                }
                else
                {
                    uFlag = 3;
                }


                List<EventFile> fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);

                gvwAssociation.DataSource = fileSearch;
                gvwAssociation.DataBind();
            }
        }

这应该有用。