English 中文(简体)
页: 1
原标题:Page.FindControl from WebServerControl

我使用“CheckBoxCounter”网络,其方法如下。 但这种方法无法在网页上找到BoxList。 现在几乎每天找回答案......你们可以帮助我......

     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>
问题回答

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.

如果<代码>GetCheckBoxListControl,你应寻找“LbxState”

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!