2009-12-21 3 views
0

WebServerControl "CheckBoxCounter"를 사용하는데 다음과 같은 방법이 있습니다. 그러나이 메서드는 페이지에서 CheckBoxList를 찾을 수 없습니다. 나는 거의 하루 동안 대답을 찾고 있습니다 ... 당신을 도울 수 있습니까? WebServerControl은 "AWT.AID.Services"네임 스페이스에 있지만 ASPX 페이지/코드에 대한 네임 스페이스가 없습니다. 이것은 결과에 영향을 미칠 것입니다.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; 
    } 

나는이

<%@ 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> 

답변

0

확인 this.CheckBoxListID의 값과 같은 웹 페이지에서이 컨트롤을 사용합니다. Whty는 당신을 ":"로 나누고 있습니다. CheckBoxList가 null로 올 것이라고 생각합니다. 실제로 올바른 ID를 찾고 있지 않기 때문에 태그 이름을 찾고 있다고 생각합니다.

GetCheckBoxListControl 경우 "LbxState"우리는, 우리가 처음 만 제어를의 ContentPlaceHolder를 찾을해야 ... MasterPages를 사용하는 경우 내가 읽은

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

어딘가를 찾고해야 내가 분할하는 이유 텍스트를 ':'로 변경하고 컨트롤 ("LbxState")을 찾기 전에 내용 자리 표시 자 ("WorkArea")를 먼저 찾습니다. 처음에 나는 이것을 시도했다 .Page.FindControl ("LbxState"),하지만 이것은 나에게도 null 참조를 얻는 것이었다 ... 이것과 어쨌든 this.FindControl ("LbxState") –

+0

CheckBoxListID의 값은 내가 지정한 것입니다. 주석 "WorkArea : LbxState" –