日期:2014-05-18  浏览次数:20491 次

菜鸟问题Repeater_ItemDataBound里如何动态的给ItemTemplate赋值
请教个问题Repeater_ItemDataBound里如何动态的给ItemTemplate赋值
<ItemTemplate> 
<a href="" class="这里是动态的">xxx </a> 
</ItemTemplate> 
总不能使用label吧?Literal试了不行(没数据),全局变量也不行
难道不能这么用?


相关代码
protected void exRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
  { 
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
  { 
  DataRowView row = (DataRowView)e.Item.DataItem;//当前行的数据 
  DateTime addtime = DateTime.Parse(row["addtime"].ToString()); 
  TimeSpan ts = DateTime.Now - addtime; 
  Label Lclass= (Label)e.Item.FindControl("class"); 
  if (ts.TotalMinutes < 60) 
  { 
  Lclass.Text = "xxx"; 
  } 
  } 
  } 


------解决方案--------------------
你可以用HTML然后把他做服务器组件来运行,可以动态赋值
------解决方案--------------------
div搞成服务器控件
------解决方案--------------------
给代码给你看看
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TemplateControlSamples
{
[ParseChildren(true)]
public class CustomRepeater : Repeater
{

// Override to prevent LiteralControls from being added as children.
protected override void AddParsedSubObject(object o)
{
}

// Override to create repeated items.
protected override void CreateChildControls()
{
object o = ViewState["NumItems"];
if (o != null)
{
// Clear any existing child controls.
Controls.Clear();

int numItems = (int)o;
for (int i = 0; i < numItems; i++)
{
// Create an item.
RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
// Initialize the item from the template.
ItemTemplate.InstantiateIn(item);
// Add the item to the ControlCollection.
Controls.Add(item);
}
}
}

// Override to create the repeated items from the DataSource.
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (DataSource != null)
{
// Iterate over an ICollection DataSource, creating a new item for each data item.
IEnumerator dataEnum = ((ICollection)base.DataSource).GetEnumerator();
int i = 0;
while (dataEnum.MoveNext())
{
// Create an item.
RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
item.DataItem = dataEnum.Current;
// Initialize the item from the template.
ItemTemplate.InstantiateIn(this);
// Add the item to the ControlCollection.
Controls.Add(item);

i++;
}

// Prevent child controls from being created again.
ChildControlsCreated = true;
// Store the number of items created in view state f