Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net中的自定义分页_C#_Asp.net - Fatal编程技术网

C# ASP.Net中的自定义分页

C# ASP.Net中的自定义分页,c#,asp.net,C#,Asp.net,我想在ASP.Net的GridView中进行自定义分页,我的数据源是一个对象数据。 我在网上查了一下,找到了一个不适合我的代码 protected void invoiceReportsgridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { if (sender != null) { displayInvListgridView.PageIndex

我想在ASP.Net的GridView中进行自定义分页,我的数据源是一个对象数据。 我在网上查了一下,找到了一个不适合我的代码

  protected void invoiceReportsgridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (sender != null)
        {
            displayInvListgridView.PageIndex = e.NewPageIndex;
            displayInvListgridView.EditIndex = -1;
            displayInvListgridView.SelectedIndex = -1;
        }
    }

    protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            CustomizePageBar(e);
        }
    }

    void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Add")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = displayInvListgridView.Rows[index];

            // Create a new ListItem object for the product in the row.     
            ListItem item = new ListItem();
            item.Text = Server.HtmlDecode(row.Cells[1].Text);

            // If the product is not already in the ListBox, add the ListItem 
            // object to the Items collection of the ListBox control. 
            //if (!displayInvListgridView.Items.Contains(item))
            //{
            //    displayInvListgridView.Items.Add(item);
            //}
        }
    }

    private void CustomizePageBar(GridViewRowEventArgs e)
    {

        //Table tblPager = new Table();
        tblPager.BorderWidth = 0;
        tblPager.CellPadding = 0;
        tblPager.CellSpacing = 0;
        tblPager.Width = Unit.Percentage(100);
        tblPager.Height = Unit.Pixel(20);

        //add a row for our pager contents
        tblPager.Rows.Add(new TableRow());

        //Spacer Cell
        TableCell tcelSpace = new TableCell();
        tcelSpace.Width = Unit.Pixel(360);


        //Page x of y Cell
        TableCell tcelXofY = new TableCell();
        tcelXofY.Width = Unit.Pixel(100);

        Label litXofY = new Label();
        litXofY.Text = "Page " + (displayInvListgridView.PageIndex + 1) + " of " + displayInvListgridView.PageCount;
        litXofY.Font.Bold = true;
        tcelXofY.Controls.Add(litXofY);


        //lable GoTo
        Label lblGoto = new Label();
        lblGoto.Text = "GoTo: ";
        lblGoto.ID = "lblGoTo";
        lblGoto.Font.Bold = true;
        lblGoto.Font.Size = displayInvListgridView.PagerStyle.Font.Size;

        TableCell tcelGoto = new TableCell();
        tcelGoto.Width = Unit.Pixel(25);
        tcelGoto.Controls.Add(lblGoto);


        //Pick drop downlist
        TableCell tcelPickPage = new TableCell();
        tcelPickPage.Width = Unit.Pixel(25);
        //The dropdown list box
        DropDownList ddlPickPage = new DropDownList();
        ddlPickPage.ID = "ddlPick";
        ddlPickPage.AutoPostBack = true;
        ddlPickPage.EnableViewState = true;
        ddlPickPage.Font.Size = displayInvListgridView.PagerStyle.Font.Size;

        for (int index = 1; index <= displayInvListgridView.PageCount; index++)
        {
            ddlPickPage.Items.Add(index.ToString());
        }
        ddlPickPage.SelectedIndex = displayInvListgridView.PageIndex;

        //handle event for picklist
        ddlPickPage.SelectedIndexChanged += new EventHandler(OnPagePicked);
        tcelPickPage.Controls.Add(ddlPickPage);

        //The existing Nav controls
        TableCell tcelNav = new TableCell();
        tcelNav.Width = Unit.Pixel(150);

        //for move all existing controls
        foreach (Control ctrl in e.Row.Cells[0].Controls)
        {
            tcelNav.Controls.Add(ctrl);
        }

        // add all cells to new pager

        tblPager.Rows[0].Cells.Add(tcelSpace);
        tblPager.Rows[0].Cells.Add(tcelXofY);
        tblPager.Rows[0].Cells.Add(tcelGoto);
        tblPager.Rows[0].Cells.Add(tcelPickPage);
        tblPager.Rows[0].Cells.Add(tcelNav);


        //replace grids pager with new
        e.Row.Cells[0].Controls.Add(tblPager);

    }


    protected void OnPagePicked(object sender, EventArgs e)
    {
        DropDownList ddlPick = (DropDownList)sender;
        displayInvListgridView.PageIndex = Convert.ToInt32(ddlPick.SelectedItem.Value) - 1;
        //Raise page index changed so user can rebind data

        GridViewPageEventArgs gvArgs = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1);
        //OnPageIndexChanging(gvArgs);
        GridViewPageEventArgs ex = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1);
        invoiceReportsgridView_PageIndexChanging(sender, gvArgs);
    }
protectedvoid invoiceReportsgridView\u页面索引交换(对象发送方,GridViewPageEventArgs e)
{
if(发送方!=null)
{
displayInvListgridView.PageIndex=e.NewPageIndex;
displayinListGridView.EditIndex=-1;
displayInvListgridView.SelectedIndex=-1;
}
}
已创建受保护的无效GridView_行(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Pager)
{
(e)临时隔离栏;
}
}
void ProductsGridView_row命令(对象发送方,GridViewCommandEventArgs e)
{
//如果GridView控件中使用了多个按钮,请使用
//CommandName属性来确定单击了哪个按钮。
如果(如CommandName==“添加”)
{
//转换存储在CommandArgument中的行索引
//属性设置为整数。
int index=Convert.ToInt32(e.CommandArgument);
//检索包含单击的按钮的行
//由用户从“行”集合中选择。
GridViewRow行=displayInvListgridView.Rows[索引];
//为行中的产品创建新的ListItem对象。
ListItem=新建ListItem();
item.Text=Server.HtmlDecode(row.Cells[1].Text);
//如果产品不在列表框中,请添加列表项
//对象添加到ListBox控件的Items集合。
//如果(!displayInvListgridView.Items.Contains(item))
//{
//displayInvListgridView.Items.Add(项目);
//}
}
}
私有void自定义PageBar(GridViewRowEventArgs e)
{
//Table tblPager=新表();
tblPager.BorderWidth=0;
tblPager.CellPadding=0;
tblPager.CellSpacing=0;
tblPager.Width=单位百分比(100);
tblPager.Height=单位像素(20);
//为我们的寻呼机内容添加一行
添加(newtableRow());
//间隔细胞
TableCell tcelSpace=新的TableCell();
tcelSpace.Width=单位像素(360);
//第x页,共y单元
TableCell tcelXofY=新的TableCell();
tcelXofY.Width=单位像素(100);
标签litXofY=新标签();
“+displayInvListgridView.PageCount”的litXofY.Text=“Page”+(displayInvListgridView.PageIndex+1)+”;
litXofY.Font.Bold=true;
tcelXofY.Controls.Add(litXofY);
//标签转到
Label lblGoto=新标签();
lblGoto.Text=“GoTo:”;
lblGoto.ID=“lblGoto”;
lblGoto.Font.Bold=true;
lblGoto.Font.Size=displayInvListgridView.PagerStyle.Font.Size;
TableCell tcelGoto=新的TableCell();
tcelGoto.Width=单位像素(25);
tcelGoto.Controls.Add(lblGoto);
//摘放下线列表
TableCell tcelPickPage=新建TableCell();
tcelPickPage.Width=单位像素(25);
//下拉列表框
DropDownList ddlPickPage=新的DropDownList();
ddlPickPage.ID=“ddlPick”;
ddlPickPage.AutoPostBack=true;
ddlPickPage.EnableViewState=true;
ddlPickPage.Font.Size=displayInvListgridView.PagerStyle.Font.Size;

对于(int index=1;index手动Gridview分页背后的思想是,每次页面更改时,您都需要重新绑定Gridview,并且还需要更改页面索引。下面是一个简单的示例:

//This is the method to bind the gridview to the data.
private void LoadGridview()
{
    List<MyObject> MyObjectList = MyObject.FillMyList();
    if (MyObjectList != null && MyObjectList.Count > 0)
    {
        this.GridView1.DataSource = MyObjectList;
        this.GridView1.DataBind();
    }
}

//When the index changes
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Changes the page
    this.GridView1.PageIndex = e.NewPageIndex;
    LoadGridview();
}
protected void Page_Load(object sender, EventArgs e)
{
    //When the page loads
    LoadGridview();
}
//这是将gridview绑定到数据的方法。
私有void LoadGridview()
{
List MyObjectList=MyObject.FillMyList();
if(MyObjectList!=null&&MyObjectList.Count>0)
{
this.GridView1.DataSource=MyObjectList;
this.GridView1.DataBind();
}
}
//当索引更改时
受保护的无效GridView1\u页面索引交换(对象发送方,GridViewPageEventArgs e)
{
//更改页面
this.GridView1.PageIndex=e.NewPageIndex;
LoadGridview();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
//当页面加载时
LoadGridview();
}

祝你好运!

请查看问题中发布的代码。你有超过几行被注释掉了——请删除不必要的行,并确保你发布的示例能够编译。如果将来有人发现这个问题,我们希望他们也能使用这里的知识。非常感谢Hanlet。我是sitin好几个小时了,再次感谢你。