使用Repeater的Template
生活随笔
收集整理的這篇文章主要介紹了
使用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>";
??????? }
總結
以上是生活随笔為你收集整理的使用Repeater的Template的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataGrid列操作
- 下一篇: 控件的呈现方法(Rendering)的内