2009-04-04 9 views
0

J'essaie de créer des répéteurs imbriqués dynamiquement en utilisant ITemplate. Ce répéteur est comme ça que je passe List<Control> et il génère un répéteur. Le problème est que lorsque je reporte le répéteur externe. Seul le dernier répéteur imbriqué s'affiche. Voici une capture d'écran. screen shot http://i40.tinypic.com/axjwpf.jpgRépéteur dynamique en C# .net

Markup


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Restricted_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <table style="border:solid 1px black;"> 
     <tr> 
      <td> 
       <asp:PlaceHolder ID="phControls" runat="server" /> 
      </td> 
     </tr> 
    </table> 
    </div> 
    </form> 
</body> 
</html> 

code Derrière



using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

public partial class Restricted_Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     CreateNestedRepeater(); 
    } 

    private void CreateNestedRepeater() 
    { 
     Repeater childRpt = new Repeater(); 
     List repeatingRuleControls = new List(); 
     repeatingRuleControls.Add(new TextBox()); 
     repeatingRuleControls.Add(new TextBox()); 
     repeatingRuleControls.Add(new TextBox()); 
     RepeatingRuleTemplate repeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls); 
     childRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls); 
     childRpt.ItemTemplate = repeatingRuleTemplate; 
     childRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null); 
     childRpt.DataSource = new DataRow[4]; 

     Repeater parentRpt = new Repeater(); 
     repeatingRuleControls = new List(); 
     repeatingRuleControls.Add(new TextBox()); 
     repeatingRuleControls.Add(new TextBox()); 
     repeatingRuleControls.Add(new TextBox()); 
     repeatingRuleControls.Add(childRpt); 
     RepeatingRuleTemplate parentrepeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls); 
     parentRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls); 
     parentRpt.ItemTemplate = parentrepeatingRuleTemplate; 
     parentRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null); 
     parentRpt.DataSource = new DataRow[4]; 
     parentRpt.DataBind(); 
     phControls.Controls.Add(parentRpt); 
    } 

    public class RepeatingRuleTemplate : ITemplate 
    { 
     ListItemType templateType; 
     List innerControls; 

     public RepeatingRuleTemplate(ListItemType type, List controls) 
     { 
      templateType = type; 
      innerControls = controls; 
     } 



     public void InstantiateIn(Control container) 
     { 
      PlaceHolder ph = new PlaceHolder(); 

      switch (templateType) 
      { 
       case ListItemType.Header: 
        ph.Controls.Add(new LiteralControl("")); 
        ph.Controls.Add(new LiteralControl("")); 
        foreach (Control control in innerControls) 
        { 
         Label label = new Label(); 
         label.Text = control.ID; 
         ph.Controls.Add(new LiteralControl("")); 
         ph.Controls.Add(label); 
         ph.Controls.Add(new LiteralControl("")); 
        } 
        ph.Controls.Add(new LiteralControl("")); 
        break; 
       case ListItemType.Item: 
        ph.Controls.Add(new LiteralControl("")); 

        foreach (Control control in innerControls) 
        { 
         if (control.GetType() != typeof(Repeater)) 
         { 
          ph.Controls.Add(new LiteralControl("")); 
          TextBox textBox = new TextBox(); 
          textBox.ID = control.ID; 
          ph.Controls.Add(textBox); 
          ph.Controls.Add(new LiteralControl("")); 
         } 
         else 
         { 
          ph.Controls.Add(new LiteralControl("")); 
          ph.Controls.Add(control as Repeater); 
          //(control as Repeater).DataSource = new DataRow[4]; 
          // (control as Repeater).DataBind(); 
          ph.Controls.Add(new LiteralControl("")); 
         } 
        } 
        ph.Controls.Add(new LiteralControl("")); 
        //ph.DataBinding += new EventHandler(Item_DataBinding); 
        break; 
       case ListItemType.Footer: 
        ph.Controls.Add(new LiteralControl("")); 
        break; 
      } 
      container.Controls.Add(ph); 
     } 



     public List Controls 
     { 
      get 
      { 
       return innerControls; 
      } 
     } 

    } 
} 

Répondre

0

Ok les gars I figured it out .... Merci à tout le monde cependant. Je faisais une erreur. La classe modèle a été fixé wrong.I et maintenant il fonctionne comme un charme follwoing est la classe

 

public class RepeatingRuleTemplate : ITemplate 
    { 
     ListItemType templateType; 
     List innerControls;

public RepeatingRuleTemplate(ListItemType type, List<Control> controls) { templateType = type; innerControls = controls; } public void InstantiateIn(Control container) { PlaceHolder ph = new PlaceHolder(); switch (templateType) { case ListItemType.Header: ph.Controls.Add(new LiteralControl("<table border=\"0\">")); ph.Controls.Add(new LiteralControl("<tr>")); foreach (Control control in innerControls) { Label label = new Label(); label.Text = control.ID; ph.Controls.Add(new LiteralControl("<td>")); ph.Controls.Add(label); ph.Controls.Add(new LiteralControl("</td>")); } ph.Controls.Add(new LiteralControl("</tr>")); break; case ListItemType.Item: ph.Controls.Add(new LiteralControl("<tr>")); foreach (Control control in innerControls) { //ph.Controls.Add(new LiteralControl("<td>")); //ph.Controls.Add(control as TextBox); //ph.Controls.Add(new LiteralControl("</td>")); if (control.GetType() != typeof(Repeater)) { ph.Controls.Add(new LiteralControl("<td>")); TextBox textBox = new TextBox(); textBox.ID = control.ID; ph.Controls.Add(textBox); ph.Controls.Add(new LiteralControl("</td>")); } else { ph.Controls.Add(new LiteralControl("<td>")); Repeater rpt = new Repeater(); rpt.DataSource = (control as Repeater).DataSource; rpt.ItemTemplate = (control as Repeater).ItemTemplate; rpt.HeaderTemplate = (control as Repeater).HeaderTemplate; rpt.FooterTemplate = (control as Repeater).FooterTemplate; rpt.DataBind(); ph.Controls.Add(rpt); //(control as Repeater).DataSource = new DataRow[4]; // (control as Repeater).DataBind(); ph.Controls.Add(new LiteralControl("</td>")); } } ph.Controls.Add(new LiteralControl("</tr>")); //ph.DataBinding += new EventHandler(Item_DataBinding); break; case ListItemType.Footer: ph.Controls.Add(new LiteralControl("</table>")); break; } container.Controls.Add(ph); } public List<Control> Controls { get { return innerControls; } } }

0

Vous devez écrire un gestionnaire pour l'événement parentRpt.ItemDataBound et définir la propriété DataSource de childRpt, puis appeler child.Rpt.DataBind() à partir de là.

+0

Selon mon appel à la connaissance DataBind est hiérarchique. Je veux dire que l'appel au parent provoquera tous les contrôles enfants à databind comme le fera. C'est ce qui est heureux. Sinon, même la dernière rangée ne s'affichera pas .. – Mohit

0

Pourquoi ne pas simplement créer un contrôle personnalisé et le remplir avec votre code?

override CreateChildControls(){ 

    foreach(var r in rows) 
    { 
    ... 
    } 
} 

Modèles, repreaters et ainsi de suite sont juste pour faire de la conception expirience mieux, dans votre situation quand vous construisez tout ce que je peux manuellement ne vois pas une raison pour laquelle overcomplicate avec tout ce que l'espace réservé, les modèles et ainsi de suite ...

Créez simplement CompositeControl et remplissez-le avec votre code. Vous allez réduire votre code deux fois et quel est le meilleur que vous aurez un code lisible. Eviter les événements désagréables pour câbler les propriétés DataSource des réinstallations enfant.

Désolé pour offtop dur ...

+0

Cela peut être une solution. Mais, cette question est petite picutre de tout le problème. Ce que j'essaie de faire, c'est de générer des rpts imbriqués frm xml.Tht xml est en fait un standard défini par notre comp pour valider les contrôles dans les répéteurs. J'essaie de générer une interface utilisateur pour tester ces règles. Donc, je dois utiliser rpts. – Mohit

+0

c'est ma première fois sur le débordement de pile.Alors je pensais que je ne peux pas commenter à nouveau. C'est pourquoi j'ai utilisé le langage de chat. Désolé. – Mohit