GridView, Repeater 자동 번호 열 증가

15984 단어 GridView
        ,

     ,   Aspx  GridView    .                   .

<Columns>         

<asp:TemplateField HeaderText="  " InsertVisible="False">

              <ItemStyle HorizontalAlign="Center" />

              <HeaderStyle HorizontalAlign="Center" Width="5%" />

             <ItemTemplate>

              <%#Container.DataItemIndex+1%>

            </ItemTemplate>

</asp:TemplateField>

</Columns>



             ,        .

            <asp:TemplateField HeaderText="  " InsertVisible="False">

              <ItemStyle HorizontalAlign="Center" />

              <HeaderStyle HorizontalAlign="Center" Width="5%" />

             <ItemTemplate>

                 <asp:Label ID="Label2" runat="server" Text='<%# this.MyListGridView.PageIndex * this.MyListGridView.PageSize + this.MyListGridView.Rows.Count + 1%>'/>

            </ItemTemplate>

            </asp:TemplateField>



     

        cs   ,      .

    <asp:BoundField HeaderText="  " >

              <ItemStyle HorizontalAlign="Center" />

              <HeaderStyle HorizontalAlign="Center" Width="5%" />

          </asp:BoundField>



        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowIndex != -1)

            {

               int indexID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;

                 e.Row.Cells[0].Text = indexID.ToString();

             }

         }

     

    GridView OnRowDataBound  ;

    GridView1_RowDataBound()      ,        :

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        //         //    http://blog.csdn.net/21aspnet 

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            ////     ,     

            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

            ////     ,     

            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

            ////      ,    ,   RowState  

            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

            //{

            //    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('      :\"" + e.Row.Cells[1].Text + "\" ?')");

            //}

        }

        if (e.Row.RowIndex != -1)

        {

            int id = e.Row.RowIndex + 1;

            e.Row.Cells[0].Text = id.ToString();

        }

    }

                  “  ”,         “  ” 。

<asp:GridView ID="GridView1" runat="server" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"

                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

                        <FooterStyle BackColor="White" ForeColor="#000066" />

                        <Columns>

                            <asp:BoundField DataField="     " HeaderText="  " ReadOnly="True" />

                            <asp:BoundField DataField="  " HeaderText="    " />

                            <asp:BoundField DataField="    " HeaderText="  " />

                            <asp:BoundField DataField="    " HeaderText="    " />

                            <asp:CommandField HeaderText="  " ShowSelectButton="True" />

                            <asp:CommandField HeaderText="  " ShowEditButton="True" />

                            <asp:CommandField HeaderText="  " ShowDeleteButton="True" />

                        </Columns>

                        <RowStyle ForeColor="#000066" />

                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />

                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

                    </asp:GridView>

             ,    ;

Repeater                ,            ,Container.ItemIndex       ,    :

<asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate><%#Container.ItemIndex %>

    </ItemTemplate>

</asp:Repeater>

        ,Repeater       ,            ,          ,      ,      1  ,            Container.ItemIndex + 1,     :

<asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate><%#Container.ItemIndex+ 1 %>

    </ItemTemplate>

</asp:Repeater><asp:RepeaterID="Repeater1"runat="server">

   <ItemTemplate>

       <%# Container.ItemIndex+1%>

       <%# (ContainerasRepeaterItem).ItemIndex+1%>

   </ItemTemplate>

</asp:Repeater>

       :

Repeater           

<FooterTemplate>  

       

<asp:Label ID="lblEmpty" Text="    " runat="server"  Visible='<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>'></asp:Label> 

                

  </FooterTemplate>

좋은 웹페이지 즐겨찾기