views:

315

answers:

1

Hi All,

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>
A: 

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;
Greg B
Somewhere I read when we are using MasterPages, we should first find the contentplaceholder and then only the control... That why I split the text of ':' and find the content place holder ("WorkArea") first, before finding the control ("LbxState"). Initially I tried this.Page.FindControl("LbxState") , but this was even fiving me null reference... Is this anyway different from this.FindControl("LbxState")
The King
The value of CheckBoxListID is exactly what I have given in the comment "WorkArea:LbxState"
The King