findControl inside formView - navigation problem?

Posted on 2007-07-20
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#.
Question by:wahooo
    LVL 12

    Expert Comment

    In which event you are using?>

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

    Are you missing "Row"


    Author Comment

    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.
    LVL 12

    Accepted Solution

    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


    Author Comment

    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.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now