none
【ASP.NET】GridView如何合并脚模板 RRS feed

  • 问题

  • 因为它不像DataList中有单独的FooterTemplate模板

    如:
    <asp:DataList id="datalist1" runat="server">
    <FooterTemplate>
    //脚模板内容
    </FooterTemplate>
    </asp:DataList>

    查看了GridView才发现GridView中的FooterTemplate只是存在于每列中。如:
    <asp:GridView ID="GvProduct" runat="server" >
    <Columns>
     <asp:TemplateField HeaderText="序号">
         <ItemTemplate>
             <%#  Container.DataItemIndex + 1%>
         </ItemTemplate>
         <FooterTemplate>脚模板内容</FooterTemplate>
     </asp:TemplateField>
    </Columns>
    </asp:GridView>

     

    因为我想在脚模板下放置分页控件,所以想合并一下

     

    • 已移动 孟宪会Moderator 2010年9月9日 3:43 (发件人:.NET Framework 一般性问题讨论区)
    2010年9月7日 6:23

答案

  • 确实不好办。你改成PagerTemplate中放


    搞定了代码如下:

        protected void GvProduct_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                TableCellCollection tcHeader = e.Row.Cells; //获取脚模板中的所有列
                tcHeader.Clear();
                tcHeader.Add(new TableCell());
                tcHeader[0].ColumnSpan=4;//跨4列
                tcHeader[0].Attributes.Add("align","center");
                HtmlGenericControl div = (HtmlGenericControl)this.Page.FindControl("fep");
                tcHeader[0].Controls.Add(div);

            }
        }

    而且我还把前台的一个div控件成功的加载到了合并后的脚模板中

    前台代码:

      <asp:GridView ID="GvProduct" runat="server" CellPadding="3" ForeColor="Black" GridLines="Vertical"
                AutoGenerateColumns="False" Width="800px" BackColor="White" BorderColor="#999999"
                BorderStyle="Solid" BorderWidth="1px" OnRowEditing="GvProduct_RowEditing" OnRowCancelingEdit="GvProduct_RowCancelingEdit"
                OnRowDataBound="GvProduct_RowDataBound" OnRowDeleting="GvProduct_RowDeleting"
                OnRowUpdating="GvProduct_RowUpdating" ShowFooter="True" OnRowCreated="GvProduct_RowCreated">
                <Columns>
                    <asp:TemplateField HeaderText="序号">
                        <ItemTemplate>
                            <%#  Container.DataItemIndex + 1%>
                        </ItemTemplate>
                        <ItemStyle Width="30px" HorizontalAlign="Center" />
                        <FooterTemplate>1</FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="用户组名称">
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="savename" Height="18px" Width="130px" Text='<%#Eval("name").ToString()%>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="name" runat="server" Text='<%# Eval("name").ToString() %>'></asp:Label></div>
                        </ItemTemplate>
                        <ItemStyle Width="100px" HorizontalAlign="Center" />
                        <HeaderStyle HorizontalAlign="Center" />
                        <FooterTemplate>2</FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="描述">
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="intro" Height="80px" Width="250px" TextMode="MultiLine"
                                Text='<%# Eval("intro").ToString()%>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <div style="width: 100%; word-break: break-all">
                                <asp:Label ID="intro" runat="server" Text='<%# Eval("intro").ToString().Length>45?Eval("intro").ToString().Substring(0,45)+".....":Eval("intro") %>'></asp:Label></div>
                        </ItemTemplate>
                        <FooterTemplate>3</FooterTemplate>
                        <ItemStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="管理">
                        <EditItemTemplate>
                            <asp:LinkButton ID="lbupdate" runat="server" CausesValidation="True" CommandName="Update"
                                Text="更新"></asp:LinkButton><br />
                            <asp:LinkButton ID="lbcancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="取消"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <span style="display: block; float: left; width: 100%;">
                                <asp:LinkButton ID="lbedit" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="修改"></asp:LinkButton>
                            </span><span style="display: block; float: left; width: 100%;" runat="server" id="showDiv">
                                <asp:LinkButton ID="lbdelete" OnClientClick="javascript:return confirm('确定删除?')"
                                    runat="server" CausesValidation="False" CommandName="Delete" Text="删除" ForeColor="#999999"></asp:LinkButton>
                            </span><a href='<%# Eval("id", "EditPopedom.aspx?id={0}") %>'>
                                <%# Convert.ToBoolean(Eval("isset"))==true?"<font color=blue>已设置</red>":"<font color=red>未设置</red>"%>
                            </a>
                        </ItemTemplate>
                        <FooterTemplate>4</FooterTemplate>
                        <ItemStyle Width="80px" HorizontalAlign="Center" />
                    </asp:TemplateField>
                   
                   
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#C7D8F1" Font-Bold="True" ForeColor="Black" CssClass="ss" />
                <AlternatingRowStyle BackColor="White" />
                <EmptyDataTemplate>
                    <div style="width: 96%; text-align: center; color: red">
                        没有数据!</div>
                </EmptyDataTemplate>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <PagerTemplate>
                    sdfsdfsdf</PagerTemplate>
            </asp:GridView>

                           <div style="text-align: center; width: 800px; float: left;" runat="server" id="fep">
                                <span style="font-size: 10pt">共
                                    <asp:HyperLink ID="LPageCount" runat="server"></asp:HyperLink></span><span style="font-size: 10pt">页
                                        当前第
                                        <asp:HyperLink ID="LCurrentPage" runat="server"></asp:HyperLink></span><span style="font-size: 10pt">页
                                        </span><span style="font-size: 10pt">&nbsp;</span><span style="font-size: 10pt"> </span>
                                <span style="font-size: 10pt">
                                    <asp:HyperLink ID="Fistpage" runat="server">首页</asp:HyperLink>
                                    <asp:HyperLink ID="Prevpage" runat="server">上一页</asp:HyperLink>
                                    <asp:HyperLink ID="Nextpage" runat="server">下一页</asp:HyperLink>
                                    <asp:HyperLink ID="Lastpage" runat="server" Font-Size="10pt">末页</asp:HyperLink></span>
                            </div>

     

    • 已标记为答案 jinwb1982 2010年9月8日 3:50
    2010年9月8日 3:50

全部回复

  • 确实不好办。你改成PagerTemplate中放
    2010年9月8日 2:52
  • 确实不好办。你改成PagerTemplate中放


    搞定了代码如下:

        protected void GvProduct_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                TableCellCollection tcHeader = e.Row.Cells; //获取脚模板中的所有列
                tcHeader.Clear();
                tcHeader.Add(new TableCell());
                tcHeader[0].ColumnSpan=4;//跨4列
                tcHeader[0].Attributes.Add("align","center");
                HtmlGenericControl div = (HtmlGenericControl)this.Page.FindControl("fep");
                tcHeader[0].Controls.Add(div);

            }
        }

    而且我还把前台的一个div控件成功的加载到了合并后的脚模板中

    前台代码:

      <asp:GridView ID="GvProduct" runat="server" CellPadding="3" ForeColor="Black" GridLines="Vertical"
                AutoGenerateColumns="False" Width="800px" BackColor="White" BorderColor="#999999"
                BorderStyle="Solid" BorderWidth="1px" OnRowEditing="GvProduct_RowEditing" OnRowCancelingEdit="GvProduct_RowCancelingEdit"
                OnRowDataBound="GvProduct_RowDataBound" OnRowDeleting="GvProduct_RowDeleting"
                OnRowUpdating="GvProduct_RowUpdating" ShowFooter="True" OnRowCreated="GvProduct_RowCreated">
                <Columns>
                    <asp:TemplateField HeaderText="序号">
                        <ItemTemplate>
                            <%#  Container.DataItemIndex + 1%>
                        </ItemTemplate>
                        <ItemStyle Width="30px" HorizontalAlign="Center" />
                        <FooterTemplate>1</FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="用户组名称">
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="savename" Height="18px" Width="130px" Text='<%#Eval("name").ToString()%>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="name" runat="server" Text='<%# Eval("name").ToString() %>'></asp:Label></div>
                        </ItemTemplate>
                        <ItemStyle Width="100px" HorizontalAlign="Center" />
                        <HeaderStyle HorizontalAlign="Center" />
                        <FooterTemplate>2</FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="描述">
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="intro" Height="80px" Width="250px" TextMode="MultiLine"
                                Text='<%# Eval("intro").ToString()%>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <div style="width: 100%; word-break: break-all">
                                <asp:Label ID="intro" runat="server" Text='<%# Eval("intro").ToString().Length>45?Eval("intro").ToString().Substring(0,45)+".....":Eval("intro") %>'></asp:Label></div>
                        </ItemTemplate>
                        <FooterTemplate>3</FooterTemplate>
                        <ItemStyle Width="200px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="管理">
                        <EditItemTemplate>
                            <asp:LinkButton ID="lbupdate" runat="server" CausesValidation="True" CommandName="Update"
                                Text="更新"></asp:LinkButton><br />
                            <asp:LinkButton ID="lbcancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="取消"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <span style="display: block; float: left; width: 100%;">
                                <asp:LinkButton ID="lbedit" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="修改"></asp:LinkButton>
                            </span><span style="display: block; float: left; width: 100%;" runat="server" id="showDiv">
                                <asp:LinkButton ID="lbdelete" OnClientClick="javascript:return confirm('确定删除?')"
                                    runat="server" CausesValidation="False" CommandName="Delete" Text="删除" ForeColor="#999999"></asp:LinkButton>
                            </span><a href='<%# Eval("id", "EditPopedom.aspx?id={0}") %>'>
                                <%# Convert.ToBoolean(Eval("isset"))==true?"<font color=blue>已设置</red>":"<font color=red>未设置</red>"%>
                            </a>
                        </ItemTemplate>
                        <FooterTemplate>4</FooterTemplate>
                        <ItemStyle Width="80px" HorizontalAlign="Center" />
                    </asp:TemplateField>
                   
                   
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#C7D8F1" Font-Bold="True" ForeColor="Black" CssClass="ss" />
                <AlternatingRowStyle BackColor="White" />
                <EmptyDataTemplate>
                    <div style="width: 96%; text-align: center; color: red">
                        没有数据!</div>
                </EmptyDataTemplate>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <PagerTemplate>
                    sdfsdfsdf</PagerTemplate>
            </asp:GridView>

                           <div style="text-align: center; width: 800px; float: left;" runat="server" id="fep">
                                <span style="font-size: 10pt">共
                                    <asp:HyperLink ID="LPageCount" runat="server"></asp:HyperLink></span><span style="font-size: 10pt">页
                                        当前第
                                        <asp:HyperLink ID="LCurrentPage" runat="server"></asp:HyperLink></span><span style="font-size: 10pt">页
                                        </span><span style="font-size: 10pt">&nbsp;</span><span style="font-size: 10pt"> </span>
                                <span style="font-size: 10pt">
                                    <asp:HyperLink ID="Fistpage" runat="server">首页</asp:HyperLink>
                                    <asp:HyperLink ID="Prevpage" runat="server">上一页</asp:HyperLink>
                                    <asp:HyperLink ID="Nextpage" runat="server">下一页</asp:HyperLink>
                                    <asp:HyperLink ID="Lastpage" runat="server" Font-Size="10pt">末页</asp:HyperLink></span>
                            </div>

     

    • 已标记为答案 jinwb1982 2010年9月8日 3:50
    2010年9月8日 3:50