2010-02-17 1 views
1

J'ai un projet avec un simple répéteur imbriqué. Lorsque je clique sur le bouton, l'événement OnItemCommand n'est pas appelé mais le OnItemCreated est appelé (cependant, il n'appelle pas le gestionnaire d'événements Page_Load). Qu'est-ce que je rate?Répéteur imbriqué .NET - Appels par clic de bouton OnItemCreated eventhandler, pas OnItemCommand

MARKUP

<table width="100%"> 
    <tr> 
     <td>my row</td> 
     <td>my description</td> 
    </tr> 
    <asp:Repeater ID="rptMain" runat="server" OnItemCreated="rptMain_ItemCreated" OnItemCommand="rptMain_ItemCommand"> 
     <ItemTemplate> 
     <tr style="background-color:#45abdc;"> 
      <td><asp:LinkButton ID="ibtnDoSomething" runat="server" CommandArgument="SELECT" Text="Clicky" /></td> 
      <td><asp:Label ID="lblMainData" runat="server"></asp:Label></td> 

     </tr> 
     <asp:Repeater ID="rptChild" runat="server" OnItemCreated="rptChild_ItemCreated"> 
      <ItemTemplate> 
       <tr style="background-color:#bcbbcb;"> 
        <td> 

        </td> 
        <td> 
         <asp:Label ID="lblChildData" runat="server"></asp:Label> 
        </td> 

       </tr> 

      </ItemTemplate> 
     </asp:Repeater> 
     </ItemTemplate> 
    </asp:Repeater> 

    </table> 

codebehind DataTable _childdata = new DataTable(); DataTable _data = new DataTable();

 public void Page_Load() 
     { 

      _data.Columns.Add(new DataColumn("Text")); 
      _data.Columns.Add(new DataColumn("CommandValue")); 

      DataRow _row1 = _data.NewRow(); 
      _row1["Text"] = "Butch"; 
      _row1["CommandValue"] = "31"; 
      DataRow _row2 = _data.NewRow(); 
      _row2["Text"] = "Karl"; 
      _row2["CommandValue"] = "2"; 
      DataRow _row3 = _data.NewRow(); 
      _row3["Text"] = "Suzie"; 
      _row3["CommandValue"] = "8"; 
      DataRow _row4 = _data.NewRow(); 
      _row4["Text"] = "Kara"; 
      _row4["CommandValue"] = "31"; 

      _data.Rows.Add(_row1); 
      _data.Rows.Add(_row2); 
      _data.Rows.Add(_row3); 
      _data.Rows.Add(_row4); 




      _childdata.Columns.Add(new DataColumn("Text")); 
      _childdata.Columns.Add(new DataColumn("CommandValue")); 

      DataRow _crow1 = _childdata.NewRow(); 
      _crow1["Text"] = "Butch"; 
      _crow1["CommandValue"] = "Cupcakes"; 
      DataRow _crow2 = _childdata.NewRow(); 
      _crow2["Text"] = "Karl"; 
      _crow2["CommandValue"] = "Cheese"; 
      DataRow _crow3 = _childdata.NewRow(); 
      _crow3["Text"] = "Suzie"; 
      _crow3["CommandValue"] = "Pizaa"; 
      DataRow _crow4 = _childdata.NewRow(); 
      _crow4["Text"] = "Kara"; 
      _crow4["CommandValue"] = "Tofu"; 
      DataRow _crow5 = _childdata.NewRow(); 
      _crow5["Text"] = "Butch"; 
      _crow5["CommandValue"] = "Bacon"; 
      DataRow _crow6 = _childdata.NewRow(); 
      _crow6["Text"] = "Karl"; 
      _crow6["CommandValue"] = "Ham"; 

      _childdata.Rows.Add(_crow1); 
      _childdata.Rows.Add(_crow2); 
      _childdata.Rows.Add(_crow3); 
      _childdata.Rows.Add(_crow4); 
      _childdata.Rows.Add(_crow5); 
      _childdata.Rows.Add(_crow6); 

      rptMain.DataSource = _data; 
      rptMain.DataBind(); 

     } 

     public void rptMain_ItemCreated(object sender, RepeaterItemEventArgs e) 
     { 

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      { 
       DataRowView drv = (DataRowView)e.Item.DataItem; 

       LinkButton ibtn = e.Item.FindControl("ibtnDoSomething") as LinkButton; 
       Label mainLabel = e.Item.FindControl("lblMainData") as Label; 
       ibtn.CommandArgument = drv["CommandValue"].ToString(); 
       mainLabel.Text = drv["Text"].ToString(); 
       DataView _dv = new DataView(_childdata, "Text = '" + drv["Text"].ToString() + "'", "Text", DataViewRowState.CurrentRows); 
       Repeater _rptChild = e.Item.FindControl("rptChild") as Repeater; 
       _rptChild.DataSource = _dv; 
       _rptChild.DataBind(); 
      } 

     } 

     public void rptChild_ItemCreated(object sender, RepeaterItemEventArgs e) 
     { 
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      { 

       Label childLabel = e.Item.FindControl("lblChildData") as Label; 
       DataRowView drv = (DataRowView)e.Item.DataItem; 
       childLabel.Text = drv["Text"].ToString(); 

      } 
     } 

     public void rptMain_ItemCommand(object sender, RepeaterCommandEventArgs e) 
     { 
      Console.Write("fdasdfsa"); 
     } 
+0

J'ai étudié plus loin et il ne marche même pas à être un répéteur imbriqué. J'ai supprimé le répéteur enfant et le clic sur le bouton appelle toujours le gestionnaire d'événements ItemCreated. Bizarre. – jimbo

Répondre

1

Le répéteur va être recréé pendant une publication. C'est pourquoi l'événement OnItemCreated se déclenche. La raison pour laquelle OnItemCommand n'est pas appelée est due à la présence d'une exception dans votre événement OnItemCreated. La propriété

e.Item.DataItem 

n'est pas disponible dans une publication et renvoie null. Donc, lorsque vous essayez d'y accéder avec

ibtn.CommandArgument = drv["CommandValue"].ToString(); 

vous vous retrouvez avec une exception NullReferenceException.

+0

J'ai ajouté une vérification pour Postback dans le Page_Load mais il casse toujours. J'ai ensuite ajouté une vérification de Postback autour de la logique ItemCreated et cela fonctionne. La chose bizarre est, le cycle de vie semble aller ItemCreated -> Page_Load -> ItemCommand. Je ne peux pas penser à n'importe quel moment où j'ai vu un acte de contrôle de répéteur comme ceci, c'est habituellement Page_Load -> ItemCommand. Je ne suis pas très sûr de ce qui cause cela. – jimbo

+0

Rappelez-vous que votre contrôle doit être recréé par asp.net au début du cycle de vie de la page. Cela se produit avant Page_Load, c'est pourquoi vous voyez l'événement ItemCreated avant l'événement Page_Load. –

0

Avez-vous besoin de spécifier un attribut CommandName sur votre contrôle linkbutton?

+1

Je l'ai ajouté pour être sûr mais je ne pense pas que ce soit nécessaire pour appeler le gestionnaire d'événement ItemCommand. – jimbo

1

Cela a fonctionné pour moi d'arrêter les cuissons postback désagréables de Item_Created

protected void On_Item_Created(Object sender, DataListItemEventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     //Do stuff 
    } 
} 
Questions connexes