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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • visio2016专业版2018最新密钥和下载方法 整理

    visio2016专业版2018最新密钥和下载方法 整理visio2016是目前微软最强大的图标制作软件,也是很难激活的。今天小编就给大家分享一下visio2016专业版的下载地址和激活方法,希望能给你带来方便。一,所有版本下载和更多有效的密钥www.dnbbn.com当然,也可以通过官网下载,登录setup.office.com.输入密钥识别后即可下载。二,激活。下载后可以输入一下密钥,但是说明一下,只适用于上述的专业版哈,其他版本无…

    2022年8月13日
    2
  • 在线打开pdm文件「建议收藏」

    在线打开pdm文件http://www.dmanywhere.cn/

    2022年4月12日
    1.4K
  • Python变量

    Python中变量类型:局部变量全局变量类变量对象变量外部变量

    2021年12月18日
    46
  • 什么是Ajax以及ajax请求的步骤[通俗易懂]

    什么是Ajax以及ajax请求的步骤[通俗易懂]什么是Ajax以及ajax请求的步骤1.Ajax是什么?AsynchronousJavaScript&XML。Ajax是web开发的一种技术。2.Ajax请求的步骤(1)创建`XMLHttpRequest`对象,也就是创建一个异步调用对象;(2)创建一个新的`HTTP`请求,并指定该`HTTP`请求的方式、`URL`及验证信息;(3)设置响应`HTTP`请求状…

    2022年5月17日
    29
  • 分子模拟软件amber_【免费】指南针模拟计算课堂第五期:邂逅分子模拟「建议收藏」

    分子模拟软件amber_【免费】指南针模拟计算课堂第五期:邂逅分子模拟「建议收藏」不知不觉,在大家的陪伴和参与下,科学指南针的免费模拟计算线上课堂,将在本周六(12.7号)来到最后一期啦!一前四期的内容,我们分别讲了计算化学的整体情况、分类的量子化学和第一性原理的基本知识普及和操作演示。错过了前四期的小伙伴,也不要失望,每期课程都有视频,文章末尾有获取前四期视频的方式,欢迎领取。二第五期的课程,我们将带大家再了解下分子模拟的相关内容。本期课程包括分子动力学和力场的一些…

    2022年5月26日
    45
  • 多重共线性检验-方差膨胀系数(VIF)

    多重共线性检验-方差膨胀系数(VIF)  方差膨胀系数(varianceinflationfactor,VIF)是衡量多元线性回归模型中复(多重)共线性严重程度的一种度量。它表示回归系数估计量的方差与假设自变量间不线性相关时方差相比的比值。  多重共线性是指自变量之间存在线性相关关系,即一个自变量可以是其他一个或几个自变量的线性组合。若存在多重共线性,计算自变量的偏回归系数时矩阵不可逆。其表现主要有:整个模型的方差分析…

    2022年6月11日
    145

发表回复

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

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