Accessing Controls Buried Inside Other Controls

I have inherited a project created by someone who no longer works at my company. We are trying to make some modifications in how the application works and I have hit a snag.

I have a formview with the following structure:
<ContentPlaceHolder>
     <table>
          <tr>
               <td>
                    <asp:FormView>
                         <InsertItemTemplate>
                              <ddlSelectDistributor>
                                   <ddlDistributor>

I want to select a value in <ddlSelectDistributor> and have that populate the dropdown <ddlDistributor> to narrow down the choices the user has to scroll through to find their desired Distributor.

I have autopostback turned on for <ddlSelectDistributor>. Once they have selected a Distributor in <ddlDistributor>, I want to populate a few label fields all within the same <InsertItemTemplate>.

I've tried all the tricks I know to try but can't get the controls to see each other. I have moved the <SqlDataSource> to inside the <InsertItemTemplate> as suggested in one of my searches, but it's just not working yet.

On the <ddlSelectDistributor.OnSelectedIndexChanged> event, I have the following code currently:

    protected void ddlSelectDistributor_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList mySD = (DropDownList)FormView1.FindControl("ddlSelectDistributor");
        DropDownList myDist = (DropDownList)FormView1.FindControl("ddlDistributor");
        SqlDataSource sqlDist = (SqlDataSource)FormView1.FindControl("sqlDistributors");
        
        if (mySD.SelectedValue == "1144")
        {
            sqlDist.SelectCommand="SELECT CUSTOMER_NUMBER + '-' + LOCATION AS CUSTOMER_NUM, CUSTOMER_NAME + ' - ' + CASE SYS_STATE WHEN ' ' THEN SYS_PROVINCE ELSE SYS_STATE END + ' ' + SYS_POSTAL_CODE + ' - ' + SYS_ADDRESS_1 AS Location FROM VW_CUSTOMER_ADDRESSES WHERE (CUSTOMER_NUMBER = '1144') ORDER BY CUSTOMER_NUMBER, LOCATION";

            myDist.DataBind();
        }

        if (mySD.SelectedValue == "2335")
        {
            sqlDist.SelectCommand="SELECT CUSTOMER_NUMBER + '-' + LOCATION AS CUSTOMER_NUM, CUSTOMER_NAME + ' - ' + CASE SYS_STATE WHEN ' ' THEN SYS_PROVINCE ELSE SYS_STATE END + ' ' + SYS_POSTAL_CODE + ' - ' + SYS_ADDRESS_1 AS Location FROM VW_CUSTOMER_ADDRESSES WHERE (CUSTOMER_NUMBER = '2335') ORDER BY CUSTOMER_NUMBER, LOCATION";

            myDist.DataBind();
        }
        
    }

Open in new window


Then on the <ddlDistributor_SelectedIndexChanged> event, there is the following code:

   protected void ddlDistributor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (((DropDownList)FormView1.Row.FindControl("ddlDistributor")).SelectedIndex == 0)
        {
            return;
        }
        string storeNbr = ((DropDownList)FormView1.Row.FindControl("ddlDistributor")).Text;
        if (storeNbr.Length > 0)
        {
            sqlSelectedDistributor.SelectParameters["CUSTOMER_NUMBER"].DefaultValue = storeNbr;
            DataView dv = (DataView)sqlSelectedDistributor.Select(DataSourceSelectArguments.Empty);
            if (dv.Table.Rows.Count == 1)
            {
                ((TextBox)FormView1.Row.FindControl("DistributorTextBox")).Text = Util.BlankForNull(dv.Table.Rows[0].ItemArray[0]);
                ((Label)FormView1.Row.FindControl("lblAddress")).Text = Util.BlankForNull(dv.Table.Rows[0].ItemArray[1]);
                ((Label)FormView1.Row.FindControl("lblStoreCSZ")).Text = Util.BlankForNull(dv.Table.Rows[0].ItemArray[2]);
                ((Label)FormView1.Row.FindControl("lblStorePhone")).Text = Util.BlankForNull(dv.Table.Rows[0].ItemArray[3]);
                ((Label)FormView1.Row.FindControl("lblStoreFax")).Text = Util.BlankForNull(dv.Table.Rows[0].ItemArray[4]);
            }
        }
        ((DropDownList)FormView1.Row.FindControl("ddlRAIssuedBy")).Focus();
    }

Open in new window


Any suggestions would be greatly appreciated.
Carla RomereDirector of Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markmiddlemistCommented:
The problem is that control are nested under their parents, and FindControl only works at the level you point it at, not the children. In pretty much every web project I have you'll find this utility function:

public static Control FindControlRecursive(Control rootControl, String controlId)
        {
            if (rootControl.ID == controlId)
                return rootControl;

            foreach (Control ChildControl in rootControl.Controls)
            {
                Control FoundControl = FindControlRecursive(ChildControl, controlId);

                if (FoundControl != null)
                    return FoundControl;
            }

            return null;
        }

Open in new window


so for instance

DropDownList mySD = (DropDownList)FormView1.FindControl("ddlSelectDistributor");

Open in new window


would become

DropDownList mySD = (DropDownList)FindControlRecursive(FormView1, "ddlSelectDistributor");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carla RomereDirector of Information TechnologyAuthor Commented:
That may be the most useful information regarding buried controls that I've seen. I will be using that little code snippet from here on out!! Thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.