0

I have a ascx control and i use FormView . On Child1 Control one dropdownlist and i want to find this parent ascx dropdownlist value on child ascx . Below is the code that i try but i am not getting the value . Always null

 FormView _parentView = this.Parent.NamingContainer as FormView;
               if (_parentView != null)
               {
                   FormViewRow _row = _parentView.Row;
                   DropDownList _ddlOrg = DropDownList)_row.FindControl("DDL_Organization");

               }

Page is Like this structrue

Parent page -- aspx

   1child control - ascx
        2child control - ascx 

i want to find 1child value on second child

Thanks for your reply .

4
  • @TimSchmelter on my child1 ascx page :(
    – A.Goutam
    Aug 14, 2014 at 12:01
  • It's still not that clear, where do you want to find the FormView? Instead of fiddling around with ..Parent..., casting and FindControl i would provide meanignful properties like Organization which you can get/set from outside. Internally this property would change the SelectedValue of the DropDownList. Aug 14, 2014 at 12:02
  • @TimSchmelter i want to find Child1.ascx dropdown on Child2.ascx
    – A.Goutam
    Aug 14, 2014 at 12:04
  • As i've mentioned in my last comment, don't hardlink these controls via FindControl("IdWwhichWillChangeSomeDayAnyway") use a property. Aug 14, 2014 at 12:06

1 Answer 1

0

The big difficulty in locating the DDL is that you are using a FormView so the controls do not exist at compile time.

Here is a property that you add to Child1 that will lookup and expose the DDL when ever you want to access it. This will be null until after you have called DataBind.

    private DropDownList z_DDL_Organization = null;
    /// <summary>
    /// Expose the Organization DDL
    /// </summary>
    public DropDownList DDL_Organization
    {
        get
        {
            if (this.z_DDL_Organization==null)
                this.z_DDL_Organization = FormView1.FindControl("DDL_Organization") as DropDownList;
            return this.z_DDL_Organization;
        }
    }

To access this in Child2 control you need to first locate the Child1 control. Because of the FormView the Child1 control will not be the immediate parent of Child2. Here is a function to search up the parent chain until it finds Child1 and also some code showing how to use it.

    protected void Button1_Click(object sender, EventArgs e)
    {
        Child1 c = LocateParent(this.Parent);
        if (c == null)
            throw new ApplicationException("Child2 does not have Child1 as a parent");

        // do stuff...
        DropDownList _ddlOrg = c.DDL_Organization;

    }

    /// <summary>
    /// Search up the parent chain to find Child1
    /// </summary>
    /// <param name="control"></param>
    /// <returns></returns>
    Child1 LocateParent( Control control)
    {
        if (control == null) return null;
        Child1 RC = control as Child1;
        if (RC != null) return RC;
        return LocateParent(control.Parent);
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.