zoukankan      html  css  js  c++  java
  • Webform中Repeater控件--绑定嵌入C#代码四种方式

    网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?>
    绑定数据的四种方式:
    1.直接绑定 <%#Eval("Code") %>
    2.调用函数 <%#ShowSex()%>
    3.显示外键关系列 <%#Eval("Nation1.Name") %>
    4.格式化显示 <%#Eval("Birthday","{0:yyyy年MM月dd日}") %>

    模板

    说明

    AlternatingItemTemplate

    与 ItemTemplate 元素类似,但在 Repeater 控件中隔行(交替项)呈现一次。通过设置 AlternatingItemTemplate 元素的样式属性,可以为其指定不同的外观。

    FooterTemplate

    在所有数据绑定行呈现之后呈现一次的元素。典型的用途是关闭在HeaderTemplate 项中打开的元素(使用 </table> 这样的标记)。

    注意   FooterTemplate 不能是数据绑定的。

    HeaderTemplate

    在所有数据绑定行呈现之前呈现一次的元素。典型的用途是开始一个容器元素(如表)。

    注意   HeaderTemplate 项不能是数据绑定的。

    ItemTemplate

    为数据源中的每一行都呈现一次的元素。若要显示 ItemTemplate 中的数据,请声明一个或多个 Web 服务器控件并设置其数据绑定表达式以使其计算为Repeater 控件(即容器控件)的 DataSource 中的字段。以下示例显示一个示例声明,它显示包含 Label 控件中的第一个名称的字段。

    First Name:
    <asp:Label runat="server"
       Text="<%# Container.DataItem.FirstName %>" />

    SeparatorTemplate

    在各行之间呈现的元素,通常是分行符(<br> 标记)、水平线(<hr> 标记)等。

    注意   SeparatorTemplate 项不能是数据绑定的。

    基本例子:

     

    aspx代码:

     1 <body>
     2     <form id="form1" runat="server">
     3     <div>
     4     
     5         <br />
     6     
     7         <br />
     8         <asp:Repeater ID="Repeater1" runat="server">
     9             <HeaderTemplate>
    10                 <table width="800" border="0" cellspacing="1" cellpadding="0" bgcolor="#6600FF">
    11                        <tr>
    12                         <td width="15%" height="35" bgcolor="#FFFFFF">代号</td>
    13                         <td width="15%" bgcolor="#FFFFFF">姓名</td>
    14                         <td width="15%" bgcolor="#FFFFFF">性别</td>
    15                         <td width="15%" bgcolor="#FFFFFF">民族</td>
    16                         <td width="15%" bgcolor="#FFFFFF">生日</td>
    17                         <td width="15%" bgcolor="#FFFFFF">操作</td>
    18                       </tr>
    19             </HeaderTemplate>
    20             <ItemTemplate>
    21                 <tr>
    22                 <td height="35" bgcolor="#FFFFFF"><%#Eval("Code") %></td>
    23                 <td bgcolor="#FFFFFF"><%#Eval("Name") %></td>
    24                 <td bgcolor="#FFFFFF"><%#ShowSex()%></td><%--嵌入函数--%>
    25                 <td bgcolor="#FFFFFF"><%#Eval("Nation1.Name") %></td>
    26                 <td bgcolor="#FFFFFF"><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
    27                 <td bgcolor="#FFFFFF"><a href="Delete.aspx?code=<%#Eval("Code") %>">删除</a>&nbsp;<a href="Update.aspx?code=<%#Eval("Code") %>">修改</a></td>
    28               </tr>
    29 
    30 
    31             </ItemTemplate>
    32             <FooterTemplate>
    33 
    34 
    35                 </table>
    36             </FooterTemplate>
    37         </asp:Repeater>
    38         <br />
    39     
    40     </div>
    41     </form>
    42 </body>
    View Code

    aspx.cs代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 
     8 public partial class test : System.Web.UI.Page
     9 {
    10     protected void Page_Load(object sender, EventArgs e)
    11     {
    12         if (!IsPostBack)
    13         {
    14             DataClassesDataContext _conect = new DataClassesDataContext();
    15 
    16             Repeater1.DataSource = _conect.Info;
    17 
    18             Repeater1.DataBind();
    19         
    20         }
    21 
    22 
    23     }
    24 
    25     public string ShowSex()
    26     {
    27         return Convert.ToBoolean(Eval("Sex"))?"":"";
    28 
    29     }
    30 }
    View Code
  • 相关阅读:
    表数据转换为insert语句
    Google Map Api 谷歌地图接口整理
    VS预生成事件命令行 和 生成后事件命令行
    C#程序开机运行
    枚举数据源化
    winform分页管理
    数据库访问驱动
    sql时间格式
    sysobjects.xtype介绍
    编码标准的多样性
  • 原文地址:https://www.cnblogs.com/franky2015/p/4873983.html
Copyright © 2011-2022 走看看