gridview自动编号

6 篇文章 0 订阅

Aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" GridLines="None" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="ItemId">

        <Columns>

            <asp:TemplateField HeaderText="编号">

                <ItemTemplate ><%# GetCount() %></ItemTemplate>

                    <HeaderStyle Width="5%" />

                </asp:TemplateField>

            <asp:TemplateField HeaderText="场馆标志">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("ImageUrl") %>'></asp:TextBox>

                </EditItemTemplate>

                <ItemTemplate>

                    <asp:Image ID="Image1" runat="server"ImageUrl='<%#"~/Upfile/" + Eval("ImageUrl") %>' Width="30" Height="28"/>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:BoundField DataField="ReportTitle" HeaderText="场地名称" />

            <asp:BoundField DataField="LinkMan" HeaderText="联系人" />

            <asp:BoundField DataField="CreatedDate" HeaderText="发布日期" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />

            <asp:BoundField DataField="ReportArea" HeaderText="面积" />

            <asp:TemplateField HeaderText="展览计划" ShowHeader="False">

                <ItemStyle BorderWidth="0px"Wrap="False"/>

                <ItemTemplate>

                <asp:LinkButton ID="LinkButton2"runat="server"CausesValidation="false"CommandName="Select"

                   Text='<%#DataBinder.Eval(Container,"DataItem.ReportExprision").ToString().Length > 10 ?DataBinder.Eval(Container,"DataItem.ReportExprision").ToString().Substring(0, 10) +"..." : DataBinder.Eval(Container,"DataItem.ReportExprision")%>'></asp:LinkButton>  

                   <%--<asp:LinkButton ID="LinkButton2"runat="server" CausesValidation="false"CommandName="Select"

                  Text='<%# DataBinder.Eval(Container,"DataItem.ProjectName").ToString().Length > 10 ?DataBinder.Eval(Container,"DataItem.ProjectExprision").ToString().Substring(0, 10) +"..." : DataBinder.Eval(Container,"DataItem.ProjectExprision")%>'></asp:LinkButton> --%>             

                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="网站链接" ShowHeader="False">

                <ItemStyle BorderWidth="0px"Wrap="False"/>

                <ItemTemplate>

                <asp:HyperLink ID="hlLink" runat="server"NavigateUrl='<%#DataBinder.Eval(Container, "DataItem.ReportWeb")%>'><%# DataBinder.Eval(Container,"DataItem.ReportWeb")%></asp:HyperLink>          

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

        <RowStyle Font-Size="13px" HorizontalAlign="Center" />

        <HeaderStyle Font-Size="14px" HorizontalAlign="Center" />

    </asp:GridView>

    <table style="width:100%;">

        <tr id="pagerTr" runat="server">

            <td align="right" style="font-size: 12px; height: 28px">

                &nbsp;<asp:Label ID="labTotal" runat="server" Text="共0条记录 第0页/共0页"></asp:Label>

                &nbsp;<asp:LinkButton ID="linkFirst" runat="server" OnClick="linkFirst_Click">首页</asp:LinkButton>

                &nbsp;<asp:LinkButton ID="linkPrevious" runat="server" OnClick="linkPrevious_Click">上一页</asp:LinkButton>

                &nbsp;<asp:LinkButton ID="linkNext" runat="server" OnClick="linkNext_Click">下一页</asp:LinkButton>

                &nbsp;<asp:LinkButton ID="linkLast" runat="server" OnClick="linkLast_Click">尾页</asp:LinkButton>

                &nbsp;

                <asp:TextBox ID="txtRows"runat="server"Width="20px"></asp:TextBox>

                &nbsp;

                <asp:LinkButton ID="linkGO" runat="server"CausesValidation="False"OnClick="linkGO_Click">GO</asp:LinkButton>

                &nbsp;</td>

        </tr>

</table>

 

.CS

#region Private Members

        publicint count=0;

        ReportControllerrc = new ReportController();

        introwcount = 10;  //要显示的行数

       

        #endregion

 

        protectedvoid Page_Load(objectsender, EventArgs e)

        {

            try

            {

                if (!Page.IsPostBack)

                {

                    ViewState[this.ID + "startPage"]= 1;

                    ViewState[this.ID + "totalPage"]= 0;

 

                    Binder();

                }

            }

            catch(Exception exc) //Modulefailed to load

            {

                Exceptions.ProcessModuleLoadException(this, exc);

            }

        }

 

        ///<summary>

        ///首页

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkFirst_Click(objectsender, EventArgs e)

        {

            ViewState[this.ID + "startPage"]= 1;

            Binder();

        }

 

        ///<summary>

        ///上一页

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkPrevious_Click(object sender, EventArgs e)

        {

            intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());

            startPage = startPage -1;

            startPage = startPage< 1 ? 1 : startPage;

            ViewState[this.ID + "startPage"]= startPage;

            Binder();

        }

 

        ///<summary>

        ///下一页

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkNext_Click(objectsender, EventArgs e)

        {

            intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());

            inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());

            startPage = startPage +1;

            startPage = startPage> totalPage ? totalPage : startPage;

            ViewState[this.ID + "startPage"]= startPage;

            Binder();

        }

 

        ///<summary>

        ///尾页

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkLast_Click(objectsender, EventArgs e)

        {

            ViewState[this.ID + "startPage"]= ViewState[this.ID + "totalPage"];

            Binder();

        }

 

        ///<summary>

        ///跳转指定页

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkGO_Click(objectsender, EventArgs e)

        {

            intcurPage = 0;

            try

            {

                curPage = int.Parse(this.txtRows.Text.Trim());

            }

            catch

            {

                this.txtRows.Text = "";

               Page.ClientScript.RegisterStartupScript(this.GetType(),"prompt", "alert('输入的页码不正确,请重新输入!')", true);

                return;

            }

 

            inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());

            curPage = curPage >totalPage ? totalPage : curPage;

            curPage = curPage < 1? 1 : curPage;

            ViewState[this.ID + "startPage"]= curPage;

            Binder();

        }

 

        ///<summary>

        ///搜索

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid linkSearch_Click(objectsender, EventArgs e)

        {

            try

            {

                Binder();

            }

            catch

            {

               Page.ClientScript.RegisterStartupScript(this.GetType(),"prompt", "alert('查询失败,请稍后再试!')", true);

            }

        }

 

        ///<summary>

        ///数据绑定

        ///</summary>

        publicvoid Binder()

        {

            intstart = (int.Parse(ViewState[this.ID + "startPage"].ToString())- 1) * rowcount;

            inttotalCount = 0;

            stringtitle = this.txtSearch.Text.Trim();

 

            List<ReportInfo> rInfo = rc.GetQueryReports(start,rowcount, title, out totalCount);

 

            this.GridView1.DataSource= rInfo;

            this.GridView1.DataBind();

 

            txtRows.Text = "";

            ViewState[this.ID + "totalPage"]= (totalCount / rowcount) + ((totalCount % rowcount) > 0 ? 1 : 0);

            labTotal.Text = "共" + totalCount.ToString() + "条记录 第" + ViewState[this.ID+ "startPage"].ToString() + "页/共" + ViewState[this.ID + "totalPage"].ToString() + "页";

           

        }

 

        ///<summary>

        ///查看

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgse)

        {

            intitemid = int.Parse(this.GridView1.DataKeys[this.GridView1.SelectedIndex].Value.ToString());

            stringurl = ModulePath + "ViewInfo.aspx?ItemId="+ itemid;

            stringscript = "<scriptlanguage='javascript'>";

            script += "window.showModalDialog('" + url + "',window,'DialogHeight=600px;dialogWidth:800px;status:no;scrollbars:auto;Resizable=no;');";

            script += "</script>";

           Page.ClientScript.RegisterStartupScript(this.GetType(),"", script);

        }

 

        publicint GetCount()

        {

            count = count + 1;

            intviewindex = int.Parse(ViewState[this.ID + "startPage"].ToString());

            return  (viewindex-1)* rowcount + count;

       }

 

重点:

public int GetCount()

        {

            count = count + 1;

            intviewindex = int.Parse(ViewState[this.ID + "startPage"].ToString());

            return  (viewindex-1)* rowcount + count;

       }

其中ViewState[this.ID+ "startPage"]表示当前页的索引,比如1就表示第一页。Rowcount表示每页显示的行数(也就是多少条数据)。Count就是编码了,每页都是从1开始的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值