ASP.NET编程方式创建中继器 [英] ASP.NET create repeater programmatically

查看:111
本文介绍了ASP.NET编程方式创建中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个ASP:直放站编程,并试图绑定列时,我有一个问题。我读过的教程是相当混乱,许多人没有得到的结合点。

I'm trying to create an ASP:Repeater programmatically and I have a problem when trying to bind the columns. The tutorials I've read are quite confusing and many of them don't get to the point of binding.

我的问题是只有在绑定数据时,我会在静态中继写了一点:

My problem is only in the point of binding data, when I would write this in a "static" repeater:

<%# DataBinder.Eval(Container.DataItem, "Name")%>

我不知道在$ C $工作的c-落后时应该是什么,这是一个类中,所以我没有一个事件处理程序。这是我的code迄今:

I don't know what should be when working in code-behind, it's within a class so I don't have an event handler. This is my code so far:

Dim DsArbol As New SqlDataAdapter(query, System.Configuration.ConfigurationManager.ConnectionStrings("CNX").ConnectionString)

    Dim tablaCarpetas As New DataTable
    DsArbol.Fill(tablaCarpetas)

    Dim RepArbol As New Repeater
    RepArbol.DataSource = tablaCarpetas
    RepArbol.ID = "repArbolCarpetas"

    Dim header As New TemplateBuilder
    Dim item As New TemplateBuilder
    Dim footer As New TemplateBuilder

    header.AppendLiteralString("<ul class=""arbol-carpetas"">")
    item.AppendLiteralString(String.Format("<li id=""li_carpeta_{0}"">{1}</li>", 1, DataBinder.Eval(Container.DataItem, "Name")))
    footer.AppendLiteralString("</ul>")

    RepArbol.HeaderTemplate = header
    RepArbol.ItemTemplate = item
    RepArbol.FooterTemplate = footer

    RepArbol.DataBind()
    PanelArbolCarpetas.Controls.Add(RepArbol)

我应该写,而不是的DataBinder.Eval(的Container.DataItem,姓名)

推荐答案

我也不太清楚关于使用 TemplateBuilder ,因为它是为消费ASP.NET框架没有可用的多文档。但是,你可以尝试改变下面行

I am not too sure about using TemplateBuilder as it is meant for consumption for ASP.NET framework and there is not much documentation available. However, you may try changing below line as

item.AppendLiteralString("<li id=\"li_carpeta_1\"><%# Eval(\"Name\") %></li>")

的另一种方法是建立自己的模板控制 - 例如:

The alternate way is to build your own template control - for example

public class MyTemplate : ITemplate
{
   ListItemType _type;

   public MyTemplate(ListItemType type)
   {
     _type = type;
   }

   public void InstantiateIn(Container control)
   {
      switch(_type)
      {
         case ListItemType.Header:
           control.Contorls.Add(new LiteralControl("<ul class=\"arbol-carpetas\">"));
           break;

         case ListItemType.Footer:
           control.Contorls.Add(new LiteralControl("</ul>"));
           break;

         case ListItemType.Item:
         case ListItemType.AlternatingItem:
            var c = new GenericHtmlControl("<li>");
            c.ID = "L";
            ... // add needed attributes etc.
            container.Controls.Add(c);
            // manage data binding
            container.DataBinding += (o,e) => 
              {
                 c.InnerText = DataBinder.Eval(Container, "Name"); 
              };
            break;
      }   
   }
}

    RepArbol.HeaderTemplate = new MyTemplate(ListItemType.Header);
    RepArbol.ItemTemplate = MyTemplate(ListItemType.Item);
    RepArbol.FooterTemplate = MyTemplate(ListItemType.Footer);
    RepArbol.DataBind()

免责声明的:未经测试code - 只是给你一个想法有关动态生成的模板和管理数据通过捕获数据绑定事件结合

Disclaimer: Untested code - just to give you an idea about building a template dynamically and manage data binding by capturing data-binding event.

这篇关于ASP.NET编程方式创建中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆