Ok ControlCollection is uncooperative

This is a discussion on Ok ControlCollection is uncooperative within the C# Programming forums, part of the General Programming Boards category; I have a section of code that I thought would work beautifully but it doesn't. It shows my control count ...

  1. #1
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107

    Ok ControlCollection is uncooperative

    I have a section of code that I thought would work beautifully but it doesn't. It shows my control count as 2, but I have 60 radio buttons, 1 label, and an image.

    I have played with the code for the past two hours and I am pretty sure I am referencing a bad page object but all the googling in the worlds seems to only confuse me more...

    So with no more ado...

    Code:
      protected void UpdateQuestionStatusLabel(object source,EventArgs e)
        {
             int i=0;
             int CtlCnt=0;
             HttpResponse resp=this.Response;
             System.Web.UI.Page currPage=this.Page;System.Web.UI.ControlCollectionctlCollection=currPage.Controls;
              //System.Web.UI.Page currPage;
              StringBuilder thelabel = new StringBuilder();
              lblStatus.Text="Hello";
          foreach(Control myCtl in currPage.Controls)
          {	
            i++;
            //lblStatus.Text="Function";
          	if(myCtl is System.Web.UI.WebControls.RadioButton)
            {
            	//thelabel.Append(myCtl.ID);  
            }
          }
          //lblStatus.Text=thelabel.ToString();
          CtlCnt=ctlCollection.Count;
    			lblStatus.Text=CtlCnt.ToString();
        }
    Thanks for all future help.
    Last edited by BillBoeBaggins; 05-20-2004 at 04:09 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I don't know much about that web stuff, but have you tried the simple version first:

    Code:
    protected void UpdateQuestionStatusLabel( object sender, EventArgs e )
    {
            lblStatus.Text = "";
    
    	foreach( Control c  in Page.Controls)
          	{	
    		lblStatus.Text += typeof(c).ToString() + ":" + c.Name + Environment.NewLine;
           	}
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Ok figured out my problem by finding one small blurb on it...
    WebControls are grouped, stacked.. whatever you want to call it so you have to search deeper through the levels..

    See below...
    Code:
    protected void UpdateQuestionStatusLabel(object source,EventArgs e)
    {
          LoopControls(Page.Controls);
          lbl.Append("Completed ");
          lbl.Append(iChecked.ToString());
          lbl.Append("/");
          i=iTotalRadios/6;
          lbl.Append(i.ToString());
          lblStatus.Text=lbl.ToString();
    }
    
    protected void LoopControls(ControlCollection Ctls)
    {
    	StringBuilder lbl= new StringBuilder();
            foreach(Control myCtl in Ctls)
    	{
    		if(myCtl is System.Web.UI.WebControls.RadioButton)
    		{
    			System.Web.UI.WebControls.RadioButton rb;
    			rb=(RadioButton)myCtl;
    			iTotalRadios++;
    			if(rb.Checked==true) iChecked++;
    		}
    		//Recursive searching, you have to cycle through the control hierachy
    		if(myCtl != null) LoopControls(myCtl.Controls);
    	}
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21