C# Repeater绑定数组,ArrayList和ArrayList实体类

本文介绍了ASP.NET中Repeater控件如何绑定数组、ArrayList以及ArrayList实体类,通过示例代码展示不同数据类型绑定的方法,并强调了不同数据绑定的区别仅在于前台代码的实现。

        ASP.NET中数据控件一个重要的功能就是绑定数据,而我们平时用的最多的绑定DataTable。但是除了DataTable之外还有一些其他数据格式也是可以绑定的!这篇博客就以Repeater为例,介绍三种绑定其他数据的方式。


1. Repeater绑定数组和ArrayList


前台代码

<body>
    <form id="form1" runat="server">
    <%--1.Repeater绑定数组--%>
    <asp:Repeater ID="rptarry" runat="server" >
        <HeaderTemplate><table></HeaderTemplate>
        <%--Repeater绑定数组前台代码绑定方式:<%# GetDataItem()%>--%>
        <ItemTemplate>
            <tr><td> <%# GetDataItem()%> </td></tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>
    
    <%--2.Repeater绑定ArrayList--%>
    <asp:Repeater ID="rptarryList" runat="server">
        <HeaderTemplate><table></HeaderTemplate>
        <%--Repeater绑定ArrayList前台代码绑定方式:<%# GetDataItem()%>--%>
        <ItemTemplate>
            <tr><td> <%# GetDataItem()%> </td></tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>
    </form>
</body>





后台代码

using System.Collections;//使用ArrayList添加的引用

namespace ArrayList测试
     {
     public partial class Repeater绑定ArrayList : System.Web.UI.Page
          {
          protected void Page_Load ( object sender, EventArgs e )
               {
               //调用绑定数组的函数
               bindrptarry ( );
               //调用绑定ArrayList的函数
               bindrptarryList ( );
               }
          //第一个函数:绑定数组
          public void bindrptarry ( )
               {
               //1.定义一个字符串
               string strs = "li|wen|yuan";
               //2.按照 | 标记将字符串分为三个字符串并赋值给str数组
               string[] str = strs.Split ( '|' );
               //3.Repeater绑定数组
               rptarry.DataSource = str;
               rptarry.DataBind ( );
               }
          //第二个函数:绑定ArrayList
          public void bindrptarryList ( )
               {
               //Repeater绑定数组ArrayList
               //调用arrayList函数获得ArrayList
               rptarry.DataSource = arrayList ( );
               rptarry.DataBind ( );
               }
          //第三个函数
          //将ArrayList中添加一些元素
          public ArrayList arrayList ( )
               {
               //1.创建ArrayList对象
               ArrayList aL = new ArrayList ( );
               //2.通过Add方法向ArrayList中添加元素
               aL.Add ( "liceshi" );
               aL.Add ( "wenceshi" );
               aL.Add ( "yuanceshi" );
               return aL;
               }
    
          }
     }






2.Repeater绑定ArrayList实体类


前台代码

<body>
    <form id="form1" runat="server">
        测试Repeater绑定ArrayList实体类
        <%--1.添加Repeater控件--%>
        <asp:Repeater ID="Repeater1" runat="server">
        <%--1.1.设置显示的表头--%>
        <HeaderTemplate>
            <table style="border:solid 1px #0094ff" >
                <tr style="border-bottom-style:solid;border:solid 1px black">
                    <%--表头分别是:学号 姓名 性别--%>
                    <td ><b>学号 </b></td>
                    <td ><b> 姓名 </b></td>
                    <td ><b> 性别 </b></td>
                </tr>
        </HeaderTemplate>
        <%--1.2.设置单元格中显示的具体数据--%>
        <ItemTemplate>
            <tr>
            <%--Repeater绑定ArrayList实体类绑定方式:<%# DataBinder.Eval(Container.DataItem, "属性名") %>--%>
                <%--学号列显示的数据--%>
                <td style="text-align:left"> <%# DataBinder.Eval(Container.DataItem, "ShuXing1") %> </td>
                <%--姓名列显示的数据--%>
                <td style="text-align:center"> <%# DataBinder.Eval(Container.DataItem, "ShuXing2") %> </td>
                <%--性别列显示的数据--%>
                <td style="text-align:center"> <%# DataBinder.Eval(Container.DataItem, "ShuXing3") %> </td>
             </tr>
            </tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>
    </form>
</body>




后台代码
using System.Collections; //使用ArrayList需要添加的引用

namespace ArrayList测试
     {
     public partial class ArrayList测试 : System.Web.UI.Page
          {
          protected void Page_Load ( object sender, EventArgs e )
               {
               if (!IsPostBack) 
                    { 
                    //1.创建ArrayList对象
                    ArrayList ceshi = new ArrayList ( );

                    //2.向ArrayList中添加实体类元素
                    for (int i = 0; i < 2; i++)//外侧循环3次
                         {
                         //内部循环的模为4---计算学号
                         int waiceng = 4 * i;
                         for (int j = 0; j < 5; j++)//内测循环5次
                              {
                              //14=外层循环次数*内层循环次数-1
                              //quanbu = i + j + 2 * j + waiceng;
                              //2.1.根据外循环和内循环的模得到学号(要求学号顺序排列 自增1)
                              int xuehao = i + j + waiceng;
                              
                              //2.2.每次循环一次就向ArrayList添加下面的元素
                              Entity yuansu = new Entity ( );
                              //学号属性的添加
                              //要求学号是按照顺序排列的
                              yuansu.ShuXing1 = xuehao.ToString();
                              //姓名属性的添加
                              yuansu.ShuXing2 = "姓名"+xuehao ;
                              //简单的根据奇数偶数来判断性别
                              if (xuehao % 2 == 0)
                                   {
                                   //当学号是偶数的时候性别是“女”
                                   yuansu.ShuXing3 = "女";
                                   }
                              else 
                                   {
                                   //当学号是奇数的时候性别是“男”
                                   yuansu.ShuXing3 = "男";
                                   }       
                              //2.3.向ArrayList中添加实体类
                              ceshi.Add ( yuansu);
                              }
                         }
                    //将ArrayList实体类ceshi绑定到Repeater1中
                    Repeater1.DataSource = ceshi;
                    Repeater1.DataBind ( );
                    }
               }
          }
     }





运行结果



3.总结

从上面的代码中可以比较出,不管是绑定数组,ArrayList还是一些其他的数据,区别只在于前台代码的绑定方式。所以其他的数据控件比如DataGridView都是和Repeater相类似的。这样子就可以更加方便的使用ASP.NET中的这些数据控件。




java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.itheima.mp.domain.po.UserInfo (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.itheima.mp.domain.po.UserInfo is in unnamed module of loader 'app') at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at com.itheima.mp.mapper.UserMapperTest.testInsert(UserMapperTest.java:64) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:231) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
最新发布
07-26
评论 21
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值