使用Repeater的Template
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)
{
foreach(string i in columnNames)
{
lc.Text += "<TD>";
lc.Text += DataBinder.Eval(container,"DataItem."+i);
lc.Text += "</TD>";
}
lc.Text += "<TR>";
}
else
{
mydataBinder(container,lc,columnNames);
}
}
}
上面的代码中,我定义了一个Delegate TemplateDataBinder ,用来控制显示输出.
使用代码:
this.Repeater1.DataSource = reader;
this.Repeater1.HeaderTemplate = new ShowDataTemplate(ListItemType.Header,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempHeader));
this.Repeater1.FooterTemplate = new ShowDataTemplate(ListItemType.Footer,DataControlerType.Repeater);
this.Repeater1.ItemTemplate = new ShowDataTemplate(ListItemType.Item,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempItem));
this.Repeater1.AlternatingItemTemplate = new ShowDataTemplate(ListItemType.AlternatingItem,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempAlternatingItem));
this.Repeater1.DataBind();
private void TempHeader (object container,Literal lc,string[] columnNames)
{
int index = -1;
lc.Text += "<table style=\"BORDER-COLLAPSE: collapse\" borderColor=\"#dcdcdc\" cellSpacing=\"0\" cellPadding=\"4\" rules=\"none\" frame=\"box\"><TR>";
foreach(string i in columnNames)
{
index++;
if(i == "ID"){
continue;
}
lc.Text += "<TD class=\"frmHeader\" style=\"BORDER-RIGHT: white 2px solid\" background=\"#dcdcdc\">" + columnShows[index] + "</TD>";
}
lc.Text += "</TR>";
}
private void TempItem (object container,Literal lc,string[] columnNames)
{
string id = "";
lc.Text += "<TR>";
foreach(string i in columnNames)
{
if(i == "ID"){
id = (string)DataBinder.Eval(container,"DataItem."+i);;
continue;
}
lc.Text += "<TD class=\"frmText\">";
if(i == "Modify")
{
lc.Text += "<a href=Modify.aspx?ID=" + id + ">修改</a>";
}
else if(i == "Name" || i == "Title"){
lc.Text += "<a href=View.aspx?ID=" + id + ">" + (string)DataBinder.Eval(container,"DataItem."+i) + "</a>";
}
else{
lc.Text += DataBinder.Eval(container,"DataItem."+i);
}
lc.Text += "</TD>";
}
lc.Text += "</TR>";
}
这里我就不多说了,可以去看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)
{
foreach(string i in columnNames)
{
lc.Text += "<TD>";
lc.Text += DataBinder.Eval(container,"DataItem."+i);
lc.Text += "</TD>";
}
lc.Text += "<TR>";
}
else
{
mydataBinder(container,lc,columnNames);
}
}
}
上面的代码中,我定义了一个Delegate TemplateDataBinder ,用来控制显示输出.
使用代码:
this.Repeater1.DataSource = reader;
this.Repeater1.HeaderTemplate = new ShowDataTemplate(ListItemType.Header,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempHeader));
this.Repeater1.FooterTemplate = new ShowDataTemplate(ListItemType.Footer,DataControlerType.Repeater);
this.Repeater1.ItemTemplate = new ShowDataTemplate(ListItemType.Item,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempItem));
this.Repeater1.AlternatingItemTemplate = new ShowDataTemplate(ListItemType.AlternatingItem,DataControlerType.Repeater,new ShowDataTemplateDelegate(TempAlternatingItem));
this.Repeater1.DataBind();
private void TempHeader (object container,Literal lc,string[] columnNames)
{
int index = -1;
lc.Text += "<table style=\"BORDER-COLLAPSE: collapse\" borderColor=\"#dcdcdc\" cellSpacing=\"0\" cellPadding=\"4\" rules=\"none\" frame=\"box\"><TR>";
foreach(string i in columnNames)
{
index++;
if(i == "ID"){
continue;
}
lc.Text += "<TD class=\"frmHeader\" style=\"BORDER-RIGHT: white 2px solid\" background=\"#dcdcdc\">" + columnShows[index] + "</TD>";
}
lc.Text += "</TR>";
}
private void TempItem (object container,Literal lc,string[] columnNames)
{
string id = "";
lc.Text += "<TR>";
foreach(string i in columnNames)
{
if(i == "ID"){
id = (string)DataBinder.Eval(container,"DataItem."+i);;
continue;
}
lc.Text += "<TD class=\"frmText\">";
if(i == "Modify")
{
lc.Text += "<a href=Modify.aspx?ID=" + id + ">修改</a>";
}
else if(i == "Name" || i == "Title"){
lc.Text += "<a href=View.aspx?ID=" + id + ">" + (string)DataBinder.Eval(container,"DataItem."+i) + "</a>";
}
else{
lc.Text += DataBinder.Eval(container,"DataItem."+i);
}
lc.Text += "</TD>";
}
lc.Text += "</TR>";
}