Repeater控件的ItemDataBound事件

Repeater控件的ItemDataBound事件Repeater控件的ItemDataBound事件:在项被绑定数据后触发。下面的例子来自msdn,不过我把前台和后台分开了。前台是:ViewCode<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="WebApplication2.WebForm1…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

Repeater控件的ItemDataBound事件:在项被绑定数据后触发。

下面的例子来自msdn,不过我把前台和后台分开了。

前台是:

Repeater控件的ItemDataBound事件
Repeater控件的ItemDataBound事件
View Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <h1>Repeater控件的ItemDataBound事件</h1>
    <form id="form1" runat="server">
    <div>

    <asp:Repeater ID="repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
      
            <HeaderTemplate>  
              <table border="1">         
                <tr>
                    <td><b>Product</b></td>
                    <td><b>Consumer Rating</b></td>
                </tr>           
            </HeaderTemplate>

            <ItemTemplate>    
                <tr>
                    <td><asp:Label Text='<%#Eval("ProductID") %>' runat="server"></asp:Label></td>
                    <td><asp:Label Text='<%#Eval("Rating") %>' ID=RatingLabel runat="server"></asp:Label></td>
                </tr>            
            </ItemTemplate>

            <FooterTemplate>
             </table>             
            </FooterTemplate>
         
        
    
    </asp:Repeater>
   
     
    </div>
    </form>
</body>
</html>

注意:table开始标签在<HeaderTemplate>中,结束标签在 <FooterTemplate>中。

绑定数据Text='<%#Eval(“ProductID”) %>’需要加单引号,里面加双引号。

后台是:

Repeater控件的ItemDataBound事件
Repeater控件的ItemDataBound事件
View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList values = new ArrayList();

            values.Add(new Evaluation("Razor Wiper Blades", "Good"));
            values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor"));
            values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair"));

            this.repeater1.DataSource = values;//指定数据源
            this.repeater1.DataBind(); //绑定数据            
        }

        protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // This event is raised for the header, the footer, separators, and items.
            // Execute the following logic for Items and Alternating Items.

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (((Evaluation)e.Item.DataItem).Rating == "Good")
                {
                    ((Label)e.Item.FindControl("RatingLabel")).Text = "<b>***Good***</b>";
                }
            }
        }

        
    }

    public class Evaluation
    {

        private string productid;
        private string rating;

        public Evaluation(string productid, string rating)
        {
            this.productid = productid;
            this.rating = rating;
        }

        public string ProductID
        {
            get
            {
                return productid;
            }
        }

        public string Rating
        {
            get
            {
                return rating;
            }
        }
    }
}

该事件在 Repeater 控件中的某一项被数据绑定后但尚未呈现在页面上之前发生。

运行结果:

 

Repeater控件的ItemDataBound事件

  

参见:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.80).aspx

 

下面说一下RepeaterItemEventArgs,它为 Repeater 的 ItemCreated 和 ItemDataBound 事件提供数据。

如果在 Repeter中有一个DropDownlist and Datalist ,然后你想根据DropDownlist的值设置Datalist的值,可以使用下面的方法来传值:

 

protected void DDLSort2_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = new DropDownList();
        dl = (DropDownList)sender;
        string sortdir = dl.SelectedValue.ToString();
        Control parent = dl.Parent;
        RepeaterItem rep = new RepeaterItem(0,ListItemType.Item);
        rep = (RepeaterItem)parent;
        RepeaterItemEventArgs e1=new RepeaterItemEventArgs(rep);
        BindInnerDatalist(sender,e1, sortdir);//另外写的方法。
    }

 

参见:http://forums.asp.net/t/1707348.aspx/1

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182471.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java中标识符的命名规则

    Java中标识符的命名规则培养好的编程习惯是十分重要的。本文带你简单认识一下Java中标识符的命名规则和规范。

    2022年7月7日
    22
  • tar打包命令(linux)

    tar打包命令(linux)1.打包命令:tar-cvf归档路径被打包文件路径。(c–createarchivev-verbosef–file,f指归档路径,故f必须放在其他选项之后,而且-可省略不写)单独打包命令:tar-cvf归档路径被打包路径.(整体文件变大,后缀一般是.tar)gzip压缩命令:tar-zcvf归档路径被打包路径.(gun-zip压缩,后缀一般是.tar.gz)bzip2压缩命令:tar-jcvf归档路径被打包路径.

    2022年5月31日
    39
  • intellij idea的快速配置详细使用

    intellij idea的快速配置详细使用IDEA实用教程一、IDEA简介1.简介IDEA全称IntelliJIDEA,是java语言开发的集成环境。IDEA是JetBrains公司的产品。JetBrains官网:https://www.jetbrains.com/IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、Ant、JUnit、CVS整合、代码审查方面。了…

    2022年5月30日
    63
  • BP神经网络算法学习及代码实现(含Python源码)[通俗易懂]

    BP神经网络算法学习及代码实现(含Python源码)[通俗易懂]目录1.写在前面2.BP神经网络推导2.1前向传播2.2反向传播2.2.1求解梯度矩阵2.2.2梯度下降法2.2.3反向传播公式推导输出层误差推导隐藏层误差参数变化率参数更新3.代码实现3.1过程解释3.1.1导入库3.1.2定义sigmoid函数3.1.3导入数据集3.1.4初始化权重和偏倚3.1.5开始训练3.2完整代码3.3预测结果1.写在前面BP神经网络算法作为作为机器学习最基础的算法,非常适合入门。透彻掌握其原理将对于今后的机器学习有很大的帮助。2.BP神经网络推导2.1前向传播前向传播

    2025年10月27日
    0
  • 在Anaconda下安装Pytorch的超详细步骤「建议收藏」

    在Anaconda下安装Pytorch的超详细步骤「建议收藏」近几年来,pytorch的发展速度越来越快,在github、CSDN等开源网站上下载的源代码逐渐由tensorflow向pytorch转变。近期在看论文时需要对代码进行复现,在安装pytorch时遇到了很多问题,将整个过程写出来供大家借鉴学习。注:在进行安装的过程中,通过cmd命令框输入命令发现找不到文件,说明环境变量没有配置好。此方法不需要配置环境变量。

    2022年10月7日
    2
  • shell sftp 命令大全「建议收藏」

    shell sftp 命令大全「建议收藏」byefinishyourSFTPsession结束会话cdchangeyourremoteworkingdirectory切换目录到指定的位置clearclearscreen清除当前屏幕内容exitfinishyourSFTPsession…

    2022年10月10日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号