CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #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