We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


findControl inside formView - navigation problem?

wahooo asked
Medium Priority
Last Modified: 2008-03-04
I have trouble accessing the controls of a formview in order to bind data to a dropdownlist using several sources for its list. It is supposed to work like this (I thought) in pageLoad of .aspx.cs-file:
DropDownList ddl = (DropDownList)FormView1.FindControl("dropDownList1");

This only produces this error:
System.NullReferenceException: Object reference not set to an instance of an object.

I tested and found that FormView1.Controls.Count had only 1 control. This control's GetType equals ChildTable. This led me to a forum which suggested my errors might be because my controls are inside the "EditItemTemplate", which will only lead me to an empty table instead of the controls I am searching for.

Below is some of my code for the formview with controls:
    <asp:FormView runat="server" ID="FormView1" DataSourceID="ObjectDataSource1">
            <div class="red">Some header text...</div>
            <div class="box">
                    <tr><td>Title:</td><td><asp:Textbox runat="server" Text='<%# Bind("Title") %>' ID="Title"></asp:Textbox></td></tr>
                        <asp:DropDownList ID="dropDownList1" runat="server" Width="200">
                        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="showPicture" checked="true" Text="Show picture" />
                        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="showPicture" checked="true" Text="Hide picture" />

Do you know how to navigate to the correct control?
I am coding in ASP.NET C#.
Watch Question

In which event you are using?>

void FormView1_ItemCreated(object sender, EventArgs e)
DropDownList ddl = (DropDownList)FormView1.Row.FindControl("dropDownList1");

Are you missing "Row"



I want the list to be loaded when the page is loaded:
    protected void Page_Load(object sender, EventArgs e)
      DropDownList ddl = (DropDownList)FormView1.Row.FindControl("dropDownList1");

I got the same error (System.NullReferenceException: Object reference not set to an instance of an object.), when I added "Row" as you suggested.
Your Dropdown list included in EditItemTemplate !! So in normal case these control does not render
So the following Line will retun null

DropDownList n = (DropDownList)FormView1.FindControl("DropDownList1");

If DropDownList1 in ItemTemplate it cold work as u expected


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


You are absolutely correct renjurdevan.
I had to put the same dropdownlist in the ItemTemplate (although I don't use it), in order to generate the list in mye aspx.cs-file. By adding defaultMode="Edit" I managed to ensure that the EditTemplate is the one used after all.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.