[REPOST]数据源不支持服务器端数据分页。我想在asp.net中的gridview中进行分页 [英] [REPOST] The data source does not support server-side data paging. I want paging in gridview in asp.net

查看:69
本文介绍了[REPOST]数据源不支持服务器端数据分页。我想在asp.net中的gridview中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是gridview的.aspx代码

 <   asp:GridView     ID   =  GridView2    runat   =  server    GridLines   =     AutoGenerateColumns   =  false    PageSize   =  1  
onrowdatabound = GridView2_RowDataBound align = center
< span class =code-attribute> OnPageIndexChanging = GridView2_PageIndexChanging AllowPaging = true >
< PagerSettings FirstPageText = first LastPageText = last PageButtonCount = 4 / >
< AlternatingRowStyle BackColor = 白色 / >
< RowStyle BackColor = #CCCCCC / >

< / asp:GridView >





这是我的.cs代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.SqlClient;

命名空间
{
public partial class 实习:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

if (!IsPostBack)
{

Recentinternship();

}



}

// 用于在gridview中获取数据

protected void Recentinternship()
{
SqlConnection con = new SqlConnection( 数据源= jayraj-pc\\sqlexpress;初始目录=实习;综合安全性=真;合并=错误);
con.Open();
string str = select B.CompanyLogo as'',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch,A.CompanyID,A。标题,A.CompanyName来自实习,作为内部联合公司AS B ON A.CompanyID = B.CompanyID;

SqlDataAdapter da = new SqlDataAdapter(str,con);
DataSet ds = new DataSet();
da.Fill(ds, 0);



GridView2.DataSource = ds.Tables [ 0 ]的ToString();
GridView2.DataBind();

con.Close();
}





受保护 void GridView2_RowDataBound( object sender,GridViewRowEventArgs e)
{
string str = 类别 + e.Row .Cells [ 6 ]。文本+ < br />持续时间: + e.Row.Cells [ 4 ]。文字+ < br /> InternshipCity: + e.Row.Cells [ 5 ]。文字+ < br /> Stipend: + e.Row.Cells [ 7 ]。文字+ < br />实习生shipStartDate: + e.Row.Cells [ 2 ]。文字+ < br /> ApplicationDeadline: + e.Row.Cells [ 3 ]。文字+ < br />所需度: + e.Row.Cells [ 9 ]。文本+ < br />必需分支: + e.Row.Cells [ 10 ]。文字;
e.Row.Cells [ 1 ]。Text = str;

// e.Row.Cells [1] .Visible = false;
// e.Row.Cells [2] .Visible = false;
e.Row.Cells [ 3 ]。可见= false ;
e.Row.Cells [ 4 ]。可见= false ;
e.Row.Cells [ 5 ]。可见= false ;
e.Row.Cells [ 6 ]。可见= false ;
e.Row.Cells [ 7 ]。可见= false ;
e.Row.Cells [ 8 ]。可见= false ;
e.Row.Cells [ 9 ]。可见= false ;
e.Row.Cells [ 10 ]。可见= false ;
e.Row.Cells [ 11 ]。可见= false ;
e.Row.Cells [ 12 ]。可见= false ;
e.Row.Cells [ 13 ]。可见= false ;


if (GridView2.Rows.Count > < span class =code-digit> 0

{
GridView2.HeaderRow.Cells [ 0 ]。Text = < span class =code-string> ;
GridView2.HeaderRow.Cells [ 1 ]。Text = ;
GridView2.HeaderRow.Cells [ 2 ]。Text = ;

}

图片img = new Image();
img.ID = abc;
img.ImageUrl = e.Row.Cells [ 0 ]。文字;
e.Row.Cells [ 0 ]。Controls.Add(img);

按钮btn = 按钮();
// btn.Text =preview;
e.Row。单元格[ 2 ]。Controls.Add(btn);
btn.CausesValidation = false ;
btn.Text = 查看并应用;
if (Session.Count > 0
{


btn.PostBackUrl = Apply_internship.aspx?InternshipID = + e.Row.Cells [ 8 ]。文字+ & CompanyID = + e.Row.Cells [ 11 ]。文字+ & Title = + e.Row.Cells [ 12 ]。文字+ & CompanyName = + e.Row.Cells [< span class =code-digit> 13 ]。文本+ ;
}

else
{

btn.PostBackUrl = Apply_login.aspx;
}

}



受保护 void GridView2_PageIndexChanging( object sender,GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
Recentinternship();
}



}
}

解决方案

使用datapager控件..





< div  class  =   pager style =   margin-left:20px; margin-top:20px; >  
< asp:DataPager ID = DataPager1 PagedControlID = GridView2 PageSize = 10 runat = server
可见= true >
< Fields>
< asp:TemplatePagerField>
< PagerTemplate>
< span style = float:right; font-size:10pt;> ; Page
< asp:标签runat = server ID = CurrentPageLabel Text = <%#Container.TotalRowCount> 0?(Container.StartRowIndex / Container.PageSize)+ 1:0%> />

< asp:标签runat = server ID = < span class =code-string>
TotalPagesLabel Text = <%#Math.Ceiling((double)Container.TotalRowCount / Container.PageSize)%> />
< / span >
< / PagerTemplate >
< / asp:TemplatePagerField >
< asp:NextPreviousPagerField ButtonCssClass = command FirstPageText = «First PreviousPageText = <上一页
RenderDisabledButtonsAsLabels = true ShowFirstPageButton = true ShowPreviousPageButton = true
ShowLastPageButton = false ShowNextPageButton = false />
< asp:NumericPagerField ButtonCount = 4 NumericButtonCssClass = command CurrentPageLabelCssClass = current
NextPreviousButtonCssClass = command />
< asp:NextPreviousPagerField ButtonCssClass = command LastPageText = Last» NextPageText = Next>
RenderDisabledButtonsAsLabels = true ShowFirstPageButton = < span class =code-string>
false ShowPreviousPageButton = false
ShowLastPageButton = true ShowNextPageButton = true />
< / 字段 >
< / asp:DataPager >
< / div >

< asp:GridView ID = GridView2 runat = server GridLines = AutoGenerateColumns = false PageSize = 1
onrowdatabound = GridView2_RowDataBound align = center
OnPageIndexChanging = GridView2_PageIndexChanging AllowPaging = true >
< AlternatingRowStyle BackColor = 白色 />
< RowStyle BackColor = #CCCCCC />

< / asp:GridView >


This is .aspx code for gridview

<asp:GridView ID="GridView2" runat="server" GridLines="None" AutoGenerateColumns="false" PageSize="1"
               onrowdatabound="GridView2_RowDataBound" align="center"
                OnPageIndexChanging="GridView2_PageIndexChanging" AllowPaging="true" >
               <PagerSettings FirstPageText="first" LastPageText="last" PageButtonCount="4" />
               <AlternatingRowStyle BackColor="White" />
               <RowStyle BackColor="#CCCCCC" />

           </asp:GridView>



This is my .cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace tables
{
    public partial class Internship : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                
                Recentinternship();
                
            }



        }

//for getting data in gridview

        protected void Recentinternship()
        {
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            string str = "select B.CompanyLogo as ' ',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch,A.CompanyID,A.Title,A.CompanyName from Internship AS A INNER JOIN  Companies AS B ON A.CompanyID=B.CompanyID ";
            
            SqlDataAdapter da = new SqlDataAdapter(str, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "0");

            

            GridView2.DataSource = ds.Tables["0"].ToString();
            GridView2.DataBind();
           
            con.Close();
        }
       
       

       

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;

            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;


            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";

            }

            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);

            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
               
                
                    btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
            
            else
            {

                btn.PostBackUrl = "Apply_login.aspx";
            }

        }

        

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            Recentinternship();
        }

       
       
    }
}

解决方案

use a datapager control..


<div class="pager" style="margin-left: 20px; margin-top: 20px;">
                        <asp:DataPager ID="DataPager1" PagedControlID="GridView2" PageSize="10" runat="server"
                            Visible="true">
                            <Fields>
                                <asp:TemplatePagerField>
                                    <PagerTemplate>
                                        <span style="float: right; font-size: 10pt;">Page
                                            <asp:Label runat="server" ID="CurrentPageLabel" Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                                            of
                                            <asp:Label runat="server" ID="TotalPagesLabel" Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                                        </span>
                                    </PagerTemplate>
                                </asp:TemplatePagerField>
                                <asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="« First" PreviousPageText="‹ Prev"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowPreviousPageButton="true"
                                    ShowLastPageButton="false" ShowNextPageButton="false" />
                                <asp:NumericPagerField ButtonCount="4" NumericButtonCssClass="command" CurrentPageLabelCssClass="current"
                                    NextPreviousButtonCssClass="command" />
                                <asp:NextPreviousPagerField ButtonCssClass="command" LastPageText="Last »" NextPageText="Next ›"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowPreviousPageButton="false"
                                    ShowLastPageButton="true" ShowNextPageButton="true" />
                            </Fields>
                        </asp:DataPager>
                    </div>

<asp:GridView ID="GridView2" runat="server" GridLines="None" AutoGenerateColumns="false" PageSize="1"
                onrowdatabound="GridView2_RowDataBound" align="center"
                 OnPageIndexChanging="GridView2_PageIndexChanging" AllowPaging="true" >
                <AlternatingRowStyle BackColor="White" />
                <RowStyle BackColor="#CCCCCC" />

            </asp:GridView>


这篇关于[REPOST]数据源不支持服务器端数据分页。我想在asp.net中的gridview中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆