Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Control Inside UserControl

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
I have an "ascx" referenced inside an aspx page.

I was using the following in the aspx page to reset certail controls to "". It worked fine, but when i decided to refactor and moved it into the referenced ascx page it stopped working. I tried adding another depth to it but I can't seem to find the correct term.

Code:
foreach (Control ctlMaster in Page.Controls)
{
    if (ctlMaster is MasterPage)
    {
        foreach (Control ctlForm in ctlMaster.Controls)
        {
            if (ctlForm is HtmlForm)
            {
                foreach (Control ctlContent in ctlForm.Controls)
                {
                    if (ctlContent is ContentPlaceHolder)
                    {
                        foreach (Control ctlChild in ctlContent.Controls)
                        {
                            if (ctlChild is TextBox)
                            {
                                if (!string.IsNullOrEmpty(ctlChild.ID))
                                {
                                    TextBox t = (TextBox)ctlContent.FindControl(ctlChild.ID);
                                    t.Text = "";
                                }
                            }

                            if (ctlChild is Label && ctlChild.ID != null)
                            {
                                Label l = (Label)ctlContent.FindControl(ctlChild.ID);
                                l.Text = "";
                            }
                        }
                    }
                }
            }
        }
    }
}

thanks for your time.
 
this is a classic example of the Arrowhead Anti-Pattern. notice how your brackets make the shape of the arrow head.

1st thing is that you shouldn't blindly reset all the controls.
unless you are dynamically adding controls, no textbox control will have an empty or null id. same goes for labels.

if you have a series of control within a web user control (ascx) and you want to reset the controls. create a public member on the web user control for the page to call. public void Reset(); within this member set all the control expliciltly to "". here's an example
Code:
<asp:TextBox ID="MyTextBox" ... />
<asp:Label ID="MyLabel" ... />
<asp:TextBox ID="MyDropDownList" ... />
Code:
public partial class MyUserControl : UserControl
{
   public int RecordId {get;set;}

   public void Page_Load(object sender, EventArgs e)
   {
      if(IsPostback) return;
      //set control values using the RecordId
   }

   public void Reset()
   {
      MyTextBox.Text = "";
      MyLabel.Text = "";
      MyDropDownList.SelectedIndex = -1;
   }
}
Code:
<custom:MyUserControl ID="InstanceOfMyUserControl" ... />
<asp:Button ID="MyButton" OnClick="MyButton_Click" ... />
Code:
public partial class MyPage : Page
{
   public void Page_Load(object sender, EventArgs e)
   {
      var id = int.Parse(Request.QueryString["key"]);
      InstanceOfMyUserControl.RecordId = id;
   }

   public void MyButton_Click(object sender, EventArgs e)
   {
       InstanceOfMyUserControl.Reset();
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top