FindControl() returns NULL when object exists in Template?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default FindControl() returns NULL when object exists in Template?

    I have seen the following behavior: when issuing a Page.FindControl() for a
    control which exists in an item template (from within an ItemDataBound()
    event, for example), I get nulls back regularly. Has anyone seen this
    before? It's pretty aggravating to have to iterate through the controls in
    each grid cell to find the ones I need, especially since finding those cells
    is not always easy. Here's my ItemDataBound() handler:

    private void streetAddressGrid_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    streetAddressGrid.EditItemIndex = e.Item.ItemIndex;
    streetAddressGrid.DataBind();
    DB.DbStreet_Address sa = new DB.DbStreet_Address();
    sa.Street_Address_ID=
    (SqlInt32)System.Convert.ToInt32((e.Item.Cells[0].Controls[1] as
    Label).Text);
    sa.SelectOne();

    // exceptions begin here; even though these controls exist, FindControl()
    returns null
    (e.Item.FindControl("line_1") as TextBox).Text = sa.Line_1.ToString();
    (e.Item.FindControl("line_2") as TextBox).Text = sa.Line_2.ToString();
    (e.Item.FindControl("line_3") as TextBox).Text = sa.Line_3.ToString();
    (e.Item.FindControl("city") as TextBox).Text = sa.City.ToString();
    (e.Item.FindControl("state") as TextBox).Text = sa.State.ToString();
    (e.Item.FindControl("postal_code") as TextBox).Text =
    sa.Postal_Code.ToString();
    (e.Item.FindControl("country") as TextBox).Text = sa.Country.ToString();
    }

    Any thoughts would be appreciated!

    Thanks,
    /s/ James


    James G. Beldock Guest

  2. Similar Questions and Discussions

    1. FindControl Method always returns empty string!!!!!
      Hi guys - Can somebody please help me? I'm a novice at all thisIve run into a serious stumbling block for the last few hours now and its killing...
    2. #26132 [Bgs]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      ID: 26132 User updated by: steven at pearavenue dot com Reported By: steven at pearavenue dot com Status: Bogus Bug...
    3. #26132 [NEW]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      From: steven at pearavenue dot com Operating system: Redhat 9.0/Apache 2.0 PHP version: 4.3.4 PHP Bug Type: PostgreSQL...
    4. OnEditCommand - .FindControl Returning Null
      cross posted in datagrid group. Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be...
    5. OnEditCommand - FindControl returning Null
      Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate>...
  3. #2

    Default Re: Solved - but WHY? (was Re: FindControl() returns NULL when object exists in Template?)

    The reason is that the FindControl() method of Control is object-specific,
    meaning that it doesn't act recursively, going down into nested levels of
    Controls within a Control. The Page object is the top-level Control, and
    other than a bunch of LiteralControls created by the HTML in the Template,
    all you will find in its Controls Collection is the runat=server Form on the
    Page.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.

    "James G. Beldock" <the word newsgroups at the domain beldock dot org> wrote
    in message news:uVi6ZmSXDHA.384@TK2MSFTNGP12.phx.gbl...
    > It appears that calling the Grid's FindControl() within the Items
    collection
    > does the trick:
    >
    > (streetAddressGrid.Items[e.Item.ItemIndex].FindControl("line_1") as
    > TextBox).Text = sa.Line_1.ToString()
    >
    > So, anyone know why?
    >
    >
    >
    > /s/ J
    >
    >
    >
    > "James G. Beldock" <the word newsgroups at the domain beldock dot org>
    wrote
    > in message news:eQ36HsRXDHA.1816@TK2MSFTNGP09.phx.gbl...
    > > I have seen the following behavior: when issuing a Page.FindControl()
    for
    > a
    > > control which exists in an item template (from within an ItemDataBound()
    > > event, for example), I get nulls back regularly. Has anyone seen this
    > > before? It's pretty aggravating to have to iterate through the controls
    > in
    > > each grid cell to find the ones I need, especially since finding those
    > cells
    > > is not always easy. Here's my ItemDataBound() handler:
    > >
    > > private void streetAddressGrid_EditCommand(object source,
    > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > > {
    > > streetAddressGrid.EditItemIndex = e.Item.ItemIndex;
    > > streetAddressGrid.DataBind();
    > > DB.DbStreet_Address sa = new DB.DbStreet_Address();
    > > sa.Street_Address_ID=
    > > (SqlInt32)System.Convert.ToInt32((e.Item.Cells[0].Controls[1] as
    > > Label).Text);
    > > sa.SelectOne();
    > >
    > > // exceptions begin here; even though these controls exist,
    FindControl()
    > > returns null
    > > (e.Item.FindControl("line_1") as TextBox).Text = sa.Line_1.ToString();
    > > (e.Item.FindControl("line_2") as TextBox).Text = sa.Line_2.ToString();
    > > (e.Item.FindControl("line_3") as TextBox).Text = sa.Line_3.ToString();
    > > (e.Item.FindControl("city") as TextBox).Text = sa.City.ToString();
    > > (e.Item.FindControl("state") as TextBox).Text = sa.State.ToString();
    > > (e.Item.FindControl("postal_code") as TextBox).Text =
    > > sa.Postal_Code.ToString();
    > > (e.Item.FindControl("country") as TextBox).Text = sa.Country.ToString();
    > > }
    > >
    > > Any thoughts would be appreciated!
    > >
    > > Thanks,
    > > /s/ James
    > >
    > >
    >
    >

    Kevin Spencer Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139