ASP.NET WebForm中asp:Repeater和UI:Grid数据为空时如何显示表头?

一、asp:Repeater

Repeater 控件用于显示被绑定在该控件上的项目的重复列表。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。

 

1.1-前台页面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<asp:Repeater ID="ImageTypeListNew" runat="server" OnItemCommand="ImageTypeListNew_ItemCommand">
                   <HeaderTemplate>
                       <table cellspacing="0" border="0" id="ctl00_contentHolder_ImageTypeList" class="table table-striped">
                           <tbody>
                               <tr>
                                   <th>分类名称</th>
                                   <th>图片数</th>
                                   <th>排序</th>
                                   <th style="width: 20%;">操作</th>
                               </tr>
                   </HeaderTemplate>
                   <ItemTemplate>
                       <tr>
                           <td>
                               <asp:HiddenField ID="hfItemIndex" runat="server" Value='<%#Container.ItemIndex %>' />
                               <asp:HiddenField ID="hfCategoryId" runat="server" Value='<%#Eval("CategoryId") %>' />
                               <Hi:HtmlDecodeTextBox ID="ImageTypeName" runat="server" Text='<%# Bind("CategoryName") %>' CssClass="forminput form-control" Visible='<%#Eval("CategoryId").ToString()!="0" %>' />
                               <asp:Label ID="ltlTypeName" runat="server" Text='<%# Bind("CategoryName") %>' Visible='<%#Eval("CategoryId").ToString()=="0" %>'></asp:Label>
                           </td>
                           <td>
                               <%#Eval("VideoCounts") %>
                           </td>
                           <td>
                               <asp:Label ID="Label1" runat="server" Text='<%# Bind("DisplaySequence") %>' Visible='<%#Eval("CategoryId").ToString()=="0" %>'></asp:Label>
                              <asp:TextBox ID="txtDisplaySequence" runat="server" Button="btnSearchButton" CssClass="forminput form-control" value='<%# Eval("DisplaySequence") %>' Visible='<%#Eval("CategoryId").ToString()!="0" %>' onkeyup="this.value=this.value.replace(/\D/g,'')"  style="width: 60px;" onafterpaste="this.value=this.value.replace(/\D/g,'')" />
                           </td>
                           <td><span class="submit_shanchu">
                               <Hi:ImageLinkButton runat="server" ID="lkbtnDelete" CommandName="Delete" IsShow="true" Text="删除" Visible='<%#Eval("CategoryId").ToString()!="0" %>'/></span></td>
                       </tr>
                   </ItemTemplate>
                   <FooterTemplate></tbody></table></FooterTemplate>
               </asp:Repeater>

 

 1.2-后台页面代码(核心)

 代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
DataTable videoCategories = GalleryHelper.GetVideoCategories(0);
DataRow row = videoCategories.NewRow();
 
//如果数据为空,设置第一行数据
row["CategoryId"] = "0";
row["CategoryName"] = "默认分类";
row["DisplaySequence"] = "0";
row["VideoCounts"] = "0";
videoCategories.Rows.InsertAt(row, 0);
 
this.ImageTypeListNew.DataSource = videoCategories;
this.ImageTypeListNew.DataBind();

 

1.3-使用方法

在页面加载时【Page_Load】可调用,以及删除/排序/编辑等场景也可调用。

 

二、UI:Grid/GridView

 显示表格数据是软件开发中的一个周期性任务。ASP.NET 提供了许多工具来在网格中显示表格数据,例如 GridView 控件。通过使用 GridView 控件,您可以显示、编辑和删除多种不同的数据源(例如数据库、XML 文件和公开数据的业务对象)中的数据。

1.1-前台页面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<UI:Grid ID="grdTopCategries" DataKeyNames="CategoryId" runat="server" ShowHeader="true" AutoGenerateColumns="false" CssClass="table table-striped" GridLines="None" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="分类名称">
            <ItemTemplate>
                <span class="icon" categoryid='<%# Eval("CategoryId") %>' parentid='<%# Eval("ParentCategoryId") %>' id="spShowImage" runat="server">
                    <img src="../images/jian.gif" width="24" height="24" /></span>
                <span class="Name" id="spCategoryName"><a href='<%# "/product_detail/"+Eval("CategoryId")%>' target="_blank">
                    <asp:Literal ID="lblCategoryName" runat="server" /></a></span>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="URL重写名称" DataField="RewriteName" />
        <asp:TemplateField HeaderText="排序">
            <ItemTemplate>
                <asp:TextBox ID="txtSequence" CssClass="form-control" runat="server" Text='<%# Eval("DisplaySequence") %>' Width="80px" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="操作" HeaderStyle-CssClass="td_left td_right_fff">
            <ItemTemplate>
                <div class="operation">
                    <span><a href="javascript:ShowRemoveProduct(<%#Eval("CategoryId") %>)">转移商品</a></span>
                      <span><span><a href="javascript:void(0);" onclick="javascript:CollectionCategory('<%# "EditReleteCategory.aspx?ParentCategoryId="+Eval("ParentCategoryId") + "&CategoryId="+Eval("CategoryId")%>')">相关课程</a></span></span>
                    <span>
                        <asp:HyperLink ID="lkEdit" runat="server" Text="编辑" NavigateUrl='<%# "EditActivityCategory.aspx?CategoryId="+Eval("CategoryId")%>'></asp:HyperLink>
                    </span>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</UI:Grid>

 

 1.2-后台页面代码(核心)

 

 代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
DataTable dt = new DataTable();
           if (inviteCodeList.TotalRecords == 0)
           {
               dt.Columns.Add("SaleId");
               dt.Columns.Add("UserId");
               dt.Columns.Add("TransactionDate");
               dt.Columns.Add("IncomeType");
               dt.Columns.Add("TransactionNo");
               dt.Columns.Add("TransactionType");
               dt.Columns.Add("RefundId");
               dt.Columns.Add("SaleName");
               dt.Columns.Add("UserName");
               dt.Columns.Add("OrderType");
               dt.Columns.Add("PaymentType");
               dt.Columns.Add("DeliverPrincipal");
               dt.Columns.Add("CommissionRate");
               dt.Columns.Add("Tax");
               dt.Columns.Add("DeliverIncome");
               dt.Columns.Add("ApprovedStatus");
               dt.Columns.Add("Reason");
               dt.Columns.Add("SettlementId");
               dt.Columns.Add("Amount");
               dt.Columns.Add("ApprovedStatusId");
 
               if (dt.Rows.Count == 0)
               {
                   dt.Rows.Add(dt.NewRow());
               }
               this.grdSaleInviteCode.DataSource = dt;
               this.grdSaleInviteCode.DataBind();
           }

 

原文链接:https://www.cnblogs.com/kimiliucn/p/17589737.html

posted @   西瓜程序猿  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(二):用.NET IoT库
· 几个自学项目的通病,别因为它们浪费了时间!
· 在外漂泊的这几年总结和感悟,展望未来
· .NET 数据拷贝方案选择
· .net工作流elsa-书签
点击右上角即可分享
微信分享提示