来自WebServerControl的Page.FindControl

时间:2009-12-21 10:24:59

标签: c# asp.net findcontrol web-controls

我使用WebServerControl“CheckBoxCounter”,它具有以下方法。但该方法无法在页面上找到CheckBoxList。我正在寻找近一天的答案......你能帮助我吗...... WebServerControl在命名空间“AWT.AID.Services”中,但ASPX页面/代码没有命名空间在后面,这会影响结果。

     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>

1 个答案:

答案 0 :(得分:0)

检查this.CheckBoxListID的值。你把它拆分为“:”。 我认为CheckBoxList是null,因为你实际上并没有找到正确的ID,我想你正在寻找标签名称。

如果GetCheckBoxListControl,您应该寻找“LbxState”

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