0

I have 8 or more Image controls each inside HyperLink and PlaceHolder I need to change parameters of each. It's for a Sitefinity gallery control. Right now I do this times 8:

if (String.IsNullOrEmpty(Image_1_File_Name) == true) {
   Image1_ph.Visible = false;
  }
  else {  
  productImageLink1.NavigateUrl = Folder_URL + Image_1_File_Name + "_l.jpg";
  productImageLink1.Attributes.Add("rel", "zoom-id:"+ zoom.ClientID +";zoom-fade: true");
  productImageLink1.Attributes.Add("rev", Folder_URL + Image_1_File_Name + "_m.jpg");
  productImage1.ImageUrl = Folder_URL + Image_1_File_Name + "_tn.jpg";
  productImage1.AlternateText = Image_1_Alt_Tag;
  }

I just copy&paste it and replace number. But I want to do this in a loop I just can't find a way to reference each set of controls.

How can this be accomplished?

2 Answers 2

4

You can use recursion to loop through various controls within panels. Here's an example.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that that example is checking for specific control types, not all controls, when disabling. Typically you would just use something like Controls[i].Enabled=false; for something like this.
and also one of the biggest complaints I have with the .Net framework. When I call .FindControl("mytextbox") . . . . Why do I have to recurse manually through the control tree? I am so tired of if( mytextbox.HasControls()){recurision }endif....</end rant>
0

Here is an approach that may work for you:

  1. Create a class that is inherited from HyperLink (Another for PlaceHolder)
  2. Add a property to the class that will allow you to set the number for the image url.
  3. Then in the Initialize function for you're derive control, you can fix your NavigateUrl (and your other properties) base on the property you created in step #2.
  4. Use your new class in the forms (instead of the HyperLink and PlaceHolder controls), in the designer set your property from step #2.

This will eliminate the need for the loop.

A google search will probably give you all kinds of help/tutorial on how to create inherited server controls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.