来自 WebServerControl 的 Page.FindControl

发布于 2024-08-15 12:45:45 字数 2622 浏览 9 评论 0原文

我使用 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>

I use a WebServerControl "CheckBoxCounter", which has the following method. But the method is unable to find the CheckBoxList on the page. I'm searching for an answer for nearly a day now... can you help me... The WebServerControl is in a namespace "AWT.AID.Services", but there is no namespace for the ASPX page / code Behind, will this affect the result.

     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;
    }

I use this control in a WebPage like this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-08-22 12:45:45

检查 this.CheckBoxListID 的值。你为什么要把它分成“:”。
我认为 CheckBoxList 为空,因为您实际上并不是在寻找正确的 ID,我认为您正在寻找标签名称。

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

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

Check the value of this.CheckBoxListID. Whty are yous splitting it on ":".
I think the CheckBoxList is coming up null because you aren't actually looking for the right ID, I think you're looking for the tag name.

If the GetCheckBoxListControl, you should be looking for "LbxState"

CheckBoxList Cbl = this.FindControl("LbxState") as CheckBoxList;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文