CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Referencing Controls on ASP.Net page

    All,

    I am populating a section of an ASP.Net page with the following code:

    Code:
    				if (catList.Count >= 1) {
    					var CLR = (CategoryLedgerRecord) catList[0];
    					cboCat1.Text = CLR.CategoryName;
    					txtAmount1.Text = CLR.Amount.ToString();
    				}
    				if (catList.Count >= 2) {
    					var CLR = (CategoryLedgerRecord) catList[1];
    					cboCat2.Text = CLR.CategoryName;
    					txtAmount2.Text = CLR.Amount.ToString();
    				}
    				if (catList.Count >= 3) {
    					var CLR = (CategoryLedgerRecord) catList[2];
    					cboCat3.Text = CLR.CategoryName;
    					txtAmount3.Text = CLR.Amount.ToString();
    				}
    				if (catList.Count >= 4) {
    					var CLR = (CategoryLedgerRecord) catList[3];
    					cboCat4.Text = CLR.CategoryName;
    					txtAmount4.Text = CLR.Amount.ToString();
    				}
    				if (catList.Count >= 5) {
    					var CLR = (CategoryLedgerRecord) catList[4];
    					cboCat5.Text = CLR.CategoryName;
    					txtAmount5.Text = CLR.Amount.ToString();
    				}
    I feel like there must be a simpler way to accomplish this. Since ASP.Net doesn't have control arrays like good 'ol VB6 did I'm having difficulty seeing how I could boil this down to a code fragment that might look like the following:

    Code:
      for (int i=0; i<5; ++i) {
        var CLR = (CategoryLedgerRecord) catList[i];
        Controls["cboCat" + (i+1)].Text = CLR.CategoryName;
        Controls["txtAmount" + (i+1)].Text = CLR.Amount.ToString();
      }
    The Controls collection only seems to allow you to reference the controls by index #, no overload for specifying a name (which would be nice). I don't see any methods to allow for this. Anyone know of a way to do this?

    This isn't a biggie ... I can manage with the first set of code. I just hate having 5 groups of the same code if I could condense it into a loop!

    Thanks,

    -Max
    If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Referencing Controls on ASP.Net page

    Then why not establish a Dictionary<string, Control> and iterate the controls by index, asking their id and referencing them?

    Code:
    foreach(Control c in container)
      dict[c.ID] = c;
    
    
    //set the control with ID BLAH to have text BOO
    dict["BLAH"].Text= "BOO";

    There is also a method called FindControl which may meet your need..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: Referencing Controls on ASP.Net page

    cjard,

    Hmm ... yeah, that may just work. Thanks for the idea - I'll give it a try!

    -Max
    If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair

  4. #4
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: Referencing Controls on ASP.Net page

    cjard,

    The idea you posted pointed me in the right direction. It took a little more work to deal with walking the control tree to get this working properly but now my library is proud owner to a new routine which will be very useful.

    Code:
    /// <summary>
    /// Web Version:
    /// 
    /// Builds a dictionary containing the references to all controls in the specified controls
    /// container so they can be referenced by constructed string names.  This provides the ability
    /// to build sections of controls and reference them similarly to the way they could have been
    /// with control arrays back in the VB6 days.
    /// </summary>
    /// <param name="container">Object containing the controls to be discovered.</param>
    /// <param name="list">Dictionary into which the references should be placed.</param>
    /// 
    public void BuildControlList(System.Web.UI.ControlCollection container, Dictionary<string, System.Web.UI.Control> list) {
    	foreach (System.Web.UI.Control c in container) {
    		//
    		//  If the current control has a set of controls, recurse and scan the collection.
    		//
    		if (c.Controls.Count > 0)
    			BuildControlList(c.Controls, list);
    		//
    		//  If this is a leaf node in the controls collection, record its reference in the
    		//  dictionary if its ID is not null.
    		//
    		else {
    			if (c.ID != null)
    				list[c.ID] = c;
    		}
    	}
    }
    Using that routine in my library now enables me to populate the "control array" I have set up in the following fashion, exactly what I had in mind.

    Code:
    //
    // There will be up to 5 categories to list.
    //
    string key = "";	// Use to build the control names to reference.
    	
    for (int i = 0; i < catList.Count; ++i) {
    	var CLR = (CategoryLedgerRecord) catList[i];
    	key = "cboCat" + (i+1).ToString();
    	((DropDownList) cList[key]).Text = CLR.CategoryName;
    	key = "txtAmount" + (i+1).ToString();
    	((TextBox) cList[key]).Text = CLR.Amount.ToString();
    	key = "txtComment" + (i+1).ToString();
    	((TextBox) cList[key]).Text = CLR.Comments;
    }
    Works perfectly. Thanks for your feedback.

    -Max
    Last edited by Max Peck; March 21st, 2013 at 09:09 AM.
    If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair

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

Featured