Bunch's Blog

One day I'll have a catchy subtitle, one day
posts - 87 , comments - 126 , trackbacks - 0

Finding Buried Controls

This post is pretty specific to an issue I had but still has some ideas that could be applied in other scenarios. The problem I had was updating a few buttons so their Text values could be set in the code behind which had a method to grab the proper value from an external source. This was so that if the application needed to be installed by a customer using a language other than English or needed a different notation for the button's Text they could simply update the database.

Most of the time this was no big deal. However I had one instance where the button was part of a control, the button had no set ID and that control was only found in a dll. So there was no markup to edit for the Button. Also updating the dll was not an option so I had to make the best of what I had to work with.

In the cs file for the aspx file with the control on it I added the Page_LoadComplete. The problem button was within a GridView so I added a foreach to go through each GridViewRow and find the button I needed. Since I did not have an ID to work with besides a random ctl00$main$DllControl$gvStuff$ctl03$ctl05 using the GridView's FindControl was out. I ended up looping through each GridViewRow, then if a RowState equaled Edit loop through the Cells, each control in the Cell and check each control to see if it held a Panel that contained the button. If the control was a Panel I could then loop through the controls in the Panel, find the Button that had text of "Update" (that was the hard coded part) and change it using the method to return the proper value from the database.

if (rowState.Contains("Edit"))
{
  foreach (DataControlFieldCell rowCell in gvr.Cells)
  {
   foreach (Control ctrl in rowCell.Controls)
   {
    if (ctrl.GetType() == typeof(Panel))
    {
     foreach (Control childCtrl in ctrl.Controls)
     {
      if (childCtrl.GetType() == typeof(Button))
      {
       Button update = (Button)childCtrl;
       if (update.Text == "Update")
       {
        update.Text = method to return the external value for the button's text;
       }
      }
     }
    }
   }
  }
}

Tags: ,

Print | posted on Wednesday, March 28, 2012 2:27 PM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: