Professional Web Applications Themes

FindControl() returns NULL when object exists in Template? - ASP.NET General

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() ...

  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. #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.384TK2MSFTNGP12.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.1816TK2MSFTNGP09.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

Similar Threads

  1. FindControl Method always returns empty string!!!!!
    By steroche in forum ASP.NET Building Controls
    Replies: 5
    Last Post: August 25th, 03:15 AM
  2. Replies: 0
    Last Post: November 5th, 11:29 PM
  3. Replies: 1
    Last Post: November 5th, 08:20 AM
  4. OnEditCommand - .FindControl Returning Null
    By George Durzi in forum ASP.NET General
    Replies: 0
    Last Post: July 14th, 09:54 PM
  5. OnEditCommand - FindControl returning Null
    By George Durzi in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: July 14th, 09:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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