Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 具有自定义GridView分页的自动序列号_C#_Asp.net_Pagination - Fatal编程技术网

C# 具有自定义GridView分页的自动序列号

C# 具有自定义GridView分页的自动序列号,c#,asp.net,pagination,C#,Asp.net,Pagination,我正在为GridView和Repeater使用自定义分页。以下是我迄今为止完成的代码: Default.aspx: <asp:Repeater ID="rptPager" runat="server"> <ItemTemplate> <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value")

我正在为GridView和Repeater使用自定义分页。以下是我迄今为止完成的代码:

Default.aspx:
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
    <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
        CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
        OnClick="lnkPage_Click" PostBackUrl='<%# "~/UI/SearchCity.aspx?page=" + Eval("Text") %>' OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

Default.aspx.cs:
private void BindGridView(int pageIndex) //Bind data
{
    List<Country> countryListView = null; //List type variable

    countryListView = aManager.AllCountryList(); //Assigns the data in the list calling the method

    totalRecordCount = countryListView.Count; //Counts total no. of record
    pageSize = 4; //Page size
    int startRow = pageIndex * pageSize; //Variable to assign the starting row

    detailsGridView.DataSource = countryListView.Skip(startRow).Take(pageSize); //Shows data in GridView
    detailsGridView.DataBind();
}

private void BindPager(int currentPageIndex) //Pagination
{
    double getPageCount = (double)((decimal)totalRecordCount / (decimal)pageSize);
    int pageCount = (int)Math.Ceiling(getPageCount); //Count page

    List<ListItem> pages = new List<ListItem>(); //New list item

    /****Pagination starts ****/
    if (pageCount > 1) 
    {
        pages.Add(new ListItem("<<", "1", currentPageIndex > 0));

        for (int i = 1; i <= pageCount; i++)
        {
            pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPageIndex + 1));
        }

        pages.Add(new ListItem(">>", pageCount.ToString(), currentPageIndex < pageCount - 1));
    }
    /****Pagination ends ****/

    rptPager.DataSource = pages;
    rptPager.DataBind();
}
Default.aspx:
Default.aspx.cs:
私有void BindGridView(int pageIndex)//绑定数据
{
List countryListView=null;//列表类型变量
countryListView=aManager.AllCountryList();//分配调用该方法的列表中的数据
totalRecordCount=countryListView.Count;//统计记录总数
pageSize=4;//页面大小
int startRow=pageIndex*pageSize;//用于分配起始行的变量
detailsGridView.DataSource=countryListView.Skip(startRow).Take(pageSize);//在GridView中显示数据
detailsGridView.DataBind();
}
私有void BindPager(int currentPageIndex)//分页
{
double getPageCount=(double)((十进制)totalRecordCount/(十进制)pageSize);
int pageCount=(int)Math.Ceiling(getPageCount);//Count page
列表页=新建列表();//新建列表项
/****分页开始****/
如果(页面计数>1)
{
添加(新列表项(“>”,pageCount.ToString(),currentPageIndex
上述方法非常有效。但问题是,当我使用以下方法生成自动序列号时,它无法正常工作:

<%#(Container.DataItemIndex+1)%>


我的意思是当我浏览到第2页时,行数从1开始,其他页面也是如此。是否有任何解决方法或其他有效的技术来处理此问题

Container.DataItemIndex是绑定到GridView的数据项的索引,可用于确定GridView行的行索引。因此,它的行为符合预期

你有两个选择: 1-使用您自己的rowcounter变量并将其存储在会话或viewpag对象中

2-更好的是,让数据库生成您的行号。例如,如果您使用的是Sql Server,请执行以下操作:

SELECT ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) ROW_NUM, * FROM MYTABLE

Container.DataItemIndex是绑定到GridView的数据项的索引,可用于确定GridView行的行索引。因此,它的行为符合预期

你有两个选择: 1-使用您自己的rowcounter变量并将其存储在会话或viewpag对象中

2-更好的是,让数据库生成您的行号。例如,如果您使用的是Sql Server,请执行以下操作:

SELECT ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) ROW_NUM, * FROM MYTABLE

以下是GridView自定义分页的解决方案:

<asp:TemplateField HeaderText="Serial Number">
    <ItemTemplate>
        <%# (detailsGridView.PageIndex * detailsGridView.PageSize) + (Container.DataItemIndex + 1) %>
     </ItemTemplate>
</asp:TemplateField>

以下是GridView自定义分页的解决方案:

<asp:TemplateField HeaderText="Serial Number">
    <ItemTemplate>
        <%# (detailsGridView.PageIndex * detailsGridView.PageSize) + (Container.DataItemIndex + 1) %>
     </ItemTemplate>
</asp:TemplateField>


我确实尝试过生成自动序列号:。最后这个,int serialNo=1;受保护的无效详细信息RidView_RowDataBound(对象发送方,GridViewRowEventArgs e){if(e.Row.RowType==DataControlRowType.DataRow){if(!String.IsNullOrEmpty(((Label)e.Row.FindControl(“labelSerialNo”).Text)){Label labelSerialNo=(Label)e.Row.FindControl(“labelSerialNo”);labelSerialNo.Text=serialNo.ToString();serialNo=serialNo+1;} } }. 但它没有起作用。如果可能,如果我错过了什么,请告诉我。我更喜欢选项2,worked.serialNo应该在页面视图中持久化。因此,您应该保存它并在需要时检索它以保持其值:
Session[“serialNo”]=serialNo
serialNo=int.Parse(会话[“serialNo”].ToString())这应该在同一页中。是否需要在会话中保留该值?如果该值在同一页中,则使用ViewState或ViewBag对象。我尝试了此操作以生成自动序列号:。最后这个,int serialNo=1;受保护的无效详细信息RidView_RowDataBound(对象发送方,GridViewRowEventArgs e){if(e.Row.RowType==DataControlRowType.DataRow){if(!String.IsNullOrEmpty(((Label)e.Row.FindControl(“labelSerialNo”).Text)){Label labelSerialNo=(Label)e.Row.FindControl(“labelSerialNo”);labelSerialNo.Text=serialNo.ToString();serialNo=serialNo+1;} } }. 但它没有起作用。如果可能,如果我错过了什么,请告诉我。我更喜欢选项2,worked.serialNo应该在页面视图中持久化。因此,您应该保存它并在需要时检索它以保持其值:
Session[“serialNo”]=serialNo
serialNo=int.Parse(会话[“serialNo”].ToString())这应该在同一页中。是否需要在会话中保留该值?如果该值位于同一页面中,则使用ViewState或ViewBag对象。