Loop through UserControls in C# ?

Results 1 to 2 of 2

Thread: Loop through UserControls in C# ?

  1. #1
    Join Date
    Dec 1969

    Default Loop through UserControls in C# ?

    I created a UserControl that is basically an image rollover. I dropped a few of them onto a page and everything worked fine. What I want to do is loop through them on Page_Load and assign attributes. I tried this code but it did not work:<BR><BR>private void Page_Load(object sender, System.EventArgs e)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;foreach(UserCon trol aCtrl in Page.Controls)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aCtrl.Attributes. Add("rolloverbox", "txtRollover");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;aCtrl.Attributes.Add("defaulttext", "This is the Default Text");<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR><BR>I got the error "Specified cast is not valid."<BR><BR>Can anyone give me a clue as to what I am doing wrong? Thanks!

  2. #2

    Default RE: Loop through UserControls in C# ?

    That might work if you had no other controls in your page.<BR><BR>foreach(Control control in Page.Controls)<BR>{<BR> if(control.GetType==typeof(YouUserControlType))<BR >{<BR>...<BR>}<BR>}

Posting Permissions

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