2009-12-21 9 views
0

J'utilise un WebServerControl "CheckBoxCounter", dont la méthode est la suivante. Mais la méthode est incapable de trouver le CheckBoxList sur la page. Je recherche une réponse depuis presque un jour maintenant ... pouvez-vous m'aider ... Le WebServerControl est dans un espace de noms "AWT.AID.Services", mais il n'y a pas d'espace de noms pour la page/code ASPX Derrière, cela affectera-t-il le résultat?Page.FindControl from WebServerControl

 protected virtual CheckBoxList GetCheckBoxListControl() 
    { 
// this.CheckBoxListID will be "WorkArea:LbxState" 

     if (string.IsNullOrEmpty(this.CheckBoxListID)) 
      throw new HttpException(string.Format("Value required for the CheckBoxListID property for the CheckBoxCounter control with ID '{0}'.", this.ID)); 

     String[] strctrl = this.CheckBoxListID.Split(':'); 
     Control ctrl= new Control(); 

     for (int i=0;i<strctrl.Length;i++) 
     { 
      if (i==0) 
      { 
       ctrl = this.Page.FindControl(strctrl[i]); 
      } 
      else 
      { 
       ctrl = ctrl.FindControl(strctrl[i]); 
      } 
      if (ctrl == null) 
      { 
       throw new HttpException(string.Format("The CheckBoxCounter control with ID '{0}' could not find a control with the ID '{1}'.", this.ID, ctrl)); 
      } 
     } 

     CheckBoxList Cbl = ctrl as CheckBoxList; 
     if (Cbl == null) 
      throw new HttpException(string.Format("The CheckBoxCounter control with ID '{0}' could not find a CheckBoxList control with the ID '{1}'.", this.ID, this.CheckBoxListID)); 

     return Cbl; 
    } 

J'utilise cette commande dans un WebPage comme celui-ci

<%@ Page Language="C#" MasterPageFile="~/Shared/Default.master" AutoEventWireup="true" CodeFile="Instructions_Add.aspx.cs" Inherits="AID_Instructions_Add" Title="Untitled Page" %> 
<%@ Register Assembly="AID" Namespace="AWT.AID.Services" TagPrefix="cc2" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="WorkArea" Runat="Server"> 
<table width="100%" border="0"> 
<tr> 
     <td class="Labl"> 
      <asp:Label ID="LblState" runat="server" Text="State"></asp:Label> 
      <cc2:CheckBoxCounter ID="CBCState" runat="server" CheckBoxListID="WorkArea:LbxState" /> 
     </td> 
     <td class="Obj" > 
      <div class="LeftOpen" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100px;"> 
      <asp:CheckBoxList ID="LbxState" CssClass="Obj" runat="server" Width="90%" DataTextField="StateName" DataValueField="StateCode" AppendDataBoundItems="True"> 
       <asp:ListItem Selected="True">ALL</asp:ListItem> 
      </asp:CheckBoxList> 
      </div></td> 
</tr> 
</table> 
</asp:Content> 

Répondre

0

Vérifiez la valeur de this.CheckBoxListID. Whyous êtes-vous divisant sur ":". Je pense que le CheckBoxList arrive à zéro parce que vous ne cherchez pas le bon ID, je pense que vous cherchez le nom du tag.

Si le GetCheckBoxListControl, vous devriez rechercher « LbxState »

CheckBoxList Cbl = this.FindControl("LbxState") as CheckBoxList; 
+0

Quelque part je lis quand nous utilisons MasterPages, nous devons d'abord trouver ContentPlaceHolder et seul le contrôle ... Ce pourquoi je partage la text de ':' et recherchez d'abord l'espace réservé au contenu ("WorkArea") avant de trouver le contrôle ("LbxState"). Initialement, j'ai essayé this.Page.FindControl ("LbxState"), mais cela me faisait même référence null ... Est-ce différent de this.FindControl ("LbxState") –

+0

La valeur de CheckBoxListID est exactement ce que j'ai donné dans le commentaire "WorkArea: LbxState" –