ASP.NET create repeater programmatically

怎甘沉沦 提交于 2019-12-24 16:25:33

问题


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")%>

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)

What should I write instead of DataBinder.Eval(Container.DataItem, "Name")?


回答1:


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()

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



来源:https://stackoverflow.com/questions/8694773/asp-net-create-repeater-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!