我知道这是可能的,但我只是不知道如何实现它。我需要从ASP.NET C#中的代码创建多个Repeater控件。我知道如何动态创建转发器以及HeaderTemplate和FooterTemplate。我遇到问题的地方是ItemTemplate。我不知道如何绑定来自DataSource的字段。我见过很多例子,但都没有。
这就是我构建HeaderTemplate的方式:
TemplateBuilder hdr = new TemplateBuilder();
hdr.AppendLiteralString("<table><thead><tr><th>Team</th></tr></thead>");
这就是我构建FooterTemplate的方式:
TemplateBuilder ftr = new TemplateBuilder();
ftr.AppendLiteralString("</table>");
这就是我将模板与转发器关联的方式:
Repeater repeater1 = new Repeater();
repeater1.DataSource = Connections.isp_GET_GRIDVIEW_DATA("STDNG", group, "", "");
repeater1.HeaderTemplate = hdr;
repeater1.ItemTemplate = dtl;
repeater1.FooterTemplate = ftr;
repeater1.DataBind();
现在我的问题在于这部分代码:
TemplateBuilder dtl = new TemplateBuilder();
dtl.AppendLiteralString(???);
如何为repeater1.ItemTemplate创建模板?如何绑定数据?
答案 0 :(得分:0)
希望你能通过这个链接找到一些东西。
http://www.codeproject.com/Articles/240760/Dynamically-create-item-templates-server-side
答案 1 :(得分:0)
我弄清楚了,不知道这是不是最好的方法,而是它的工作
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.ComponentModel;
/// <summary>
/// Summary description for Repeater_Templates
/// </summary>
public class Repeater_Templates : ITemplate
{
static int itemcount = 0;
ListItemType templateType;
public Repeater_Templates(ListItemType type)
{
templateType = type;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch( templateType )
{
case ListItemType.Header:
lc.Text = "<table class=\"table zebra-striped\"><thead><tr><th>Equipo</th><th>J</th><th>G</th><th>E</th><th>P</th><th>G</th><th>E</th><th>D</th><th>PTS</th></tr></thead><tbody><tr>";
break;
case ListItemType.Item:
lc.Text = "<td>";
lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
break;
case ListItemType.AlternatingItem:
lc.Text = "<td>";
lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
break;
case ListItemType.Footer:
lc.Text = "</tbody></table>";
break;
}
container.Controls.Add(lc);
itemcount += 1;
}
private void TemplateControl_DataBinding(object sender, System.EventArgs e)
{
Literal lc;
lc = (Literal)sender;
RepeaterItem container = (RepeaterItem)lc.NamingContainer;
lc.Text += "<img style=\"vertical-align:middle\" src=\"" + DataBinder.Eval(container.DataItem, "LOCATION") + "\" /> "
+ DataBinder.Eval(container.DataItem, "TEAM") + "</td><td>" + DataBinder.Eval(container.DataItem, "GAMES") + "</td><td>" + DataBinder.Eval(container.DataItem, "WON")
+ "</td><td>" + DataBinder.Eval(container.DataItem, "DRAW") + "</td><td>" + DataBinder.Eval(container.DataItem, "LOST") + "</td><td>" + DataBinder.Eval(container.DataItem, "GOALS")
+ "</td><td>" + DataBinder.Eval(container.DataItem, "AGAINST") + "</td><td>" + DataBinder.Eval(container.DataItem, "DIFFERENCE") + "</td><td>" + DataBinder.Eval(container.DataItem, "POINTS");
lc.Text += "</td><tr>";
}
}
希望有所帮助