Results 1 to 3 of 3

Thread: [RESOLVED] Ok ControlCollection is uncooperative

  1. #1

    Thread Starter
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Resolved [RESOLVED] 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();
        }

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    The only thing that comes to mind is that your Page object is not the parent for your 60 radio buttons. If you have those controls as children for some other control, then you would have to loop through the Controls collection of the parent control.

  3. #3

    Thread Starter
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628
    You are right on the money:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width