程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 使用Repeater的Template 基礎教程

使用Repeater的Template 基礎教程

編輯:關於C#
 

.NET FX提供了一個方法,就是使用Template來在程序運行時對Repeater的顯示進行控制.
這裡我就不多說了,可以去看MSDN中的相關資料.(嘿嘿,其實我也沒搞多少)..

其實主要是private void lc_DataBinding(object sender, EventArgs e)函數,在DataSource中的數據進行Bind的時候,每Bind一個,都會調用這個函數一次,我們就通過自定義這個函數的實現來完成動態修改格局:)

下面是代碼的實現部分:
/// <summary>
/// DataControlerType
/// </summary>
public enum DataControlerType
{
/// <summary>
/// Repeater
/// </summary>
Repeater,
/// <summary>
/// DataList
/// </summary>
DataList,
/// <summary>
/// DataGrid
/// </summary>
DataGrid
}

// public delegate void DataBinder(object container,Literal lc);

/// <summary>
/// DataBinder handler.
/// </summary>
public delegate void TemplateDataBinder(object container,Literal lc,string[] columnnames);

/// <summary>
/// Summary description for Template.
/// </summary>
public class DataTemplateItem : ITemplate
{
private ListItemType templateType;
private string [] columnNames = null;
private DataControlerType controlType;
public TemplateDataBinder mydataBinder = null;


/// <summary>
/// Constructor
/// </summary>
/// <param name="columnnames"></param>
/// <param name="templatetype"></param>
/// <param name="controltype"></param>
public DataTemplateItem(string [] columnnames,ListItemType templatetype,DataControlerType controltype)
{
templateType = templatetype;
columnNames = columnnames;
controlType = controltype;
}

/// <summary>
///
/// </summary>
/// <param name="columnnames"></param>
/// <param name="templatetype"></param>
/// <param name="controltype"></param>
/// <param name="dataBinder"></param>
public DataTemplateItem(string [] columnnames,ListItemType templatetype,DataControlerType controltype,TemplateDataBinder dataBinder)
{
mydataBinder = dataBinder;
templateType = templatetype;
columnNames = columnnames;
controlType = controltype;
}

/// <summary>
///
/// </summary>
/// <param name="templatetype"></param>
/// <param name="controltype"></param>
public DataTemplateItem(ListItemType templatetype,DataControlerType controltype)
{
templateType = templatetype;
controlType = controltype;
}

/// <summary>
///
/// </summary>
/// <param name="templatetype"></param>
/// <param name="controltype"></param>
/// <param name="dataBinder"></param>
public DataTemplateItem(ListItemType templatetype,DataControlerType controltype,TemplateDataBinder dataBinder)
{
mydataBinder = dataBinder;
templateType = templatetype;
controlType = controltype;
}

public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
if(mydataBinder == null)
{
switch(templateType)
{
case ListItemType.Header:
lc.Text = "<TABLE><TR>";
foreach(string i in columnNames)
{
lc.Text += "<TD>" + i + "</TD>";
}
lc.Text += "</TR>";
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
break;
case ListItemType.Item:
case ListItemType.AlternatingItem:
lc.DataBinding += new EventHandler(lc_DataBinding);
break;
}
}
else
{
switch(templateType)
{
case ListItemType.Header:
case ListItemType.Footer:
mydataBinder(container,lc,this.columnNames);
break;
case ListItemType.Item:
case ListItemType.AlternatingItem:
lc.DataBinding += new EventHandler(lc_DataBinding);
break;
}
}
container.Controls.Add(lc);
}

private void lc_DataBinding(object sender, EventArgs e)
{
Literal lc = (Literal)sender;
Object container;
if(controlType == DataControlerType.Repeater)
{
container = (RepeaterItem)lc.NamingContainer;
}
else if(controlType == DataControlerType.DataList)
{
container = (DataListItem)lc.NamingContainer;
}
else //if(controlType == ControlType.DataGrid)
{
container = (DataGridItem)lc.NamingContainer;
}

if(mydataBinder == null)  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved