[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1827
  • Last Modified:

findControl inside formView - navigation problem?

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#.
0
wahooo
Asked:
wahooo
  • 2
  • 2
1 Solution
 
renjurdevanCommented:
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
0
 
wahoooAuthor 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.
0
 
renjurdevanCommented:
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
0
 
wahoooAuthor 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!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now