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

x

findControl inside formView - navigation problem?

wahooo
wahooo asked
on
Medium Priority
1,886 Views
Last Modified: 2008-03-04
Hi.
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">
        <EditItemTemplate>
            <div class="red">Some header text...</div>
            <div class="box">
                <table>
                    <tr><td>Title:</td><td><asp:Textbox runat="server" Text='<%# Bind("Title") %>' ID="Title"></asp:Textbox></td></tr>
                    <tr><td>List:</td><td>
                        <asp:DropDownList ID="dropDownList1" runat="server" Width="200">
                        </asp:DropDownList>
                    </td></tr>
                    <tr><td>Radios:</td><td>
                        <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" />
                    </td></tr>
                </table>
            </div>    
        </EditItemTemplate>
    </asp:FormView>

Do you know how to navigate to the correct control?
I am coding in ASP.NET C#.
Comment
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"

Regards
Renju

Author

Commented:
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

Regards
Renju

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

Ask the Experts

Author

Commented:
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.

Thanks!
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.

OR

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.