Ask a Question related to ASP.NET General, Design and Development.
-
James G. Beldock #1
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
-
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... -
#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... -
#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... -
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... -
OnEditCommand - FindControl returning Null
Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate>... -
Kevin Spencer #2
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...collection> It appears that calling the Grid's FindControl() within the Itemswrote> 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>for> in message news:eQ36HsRXDHA.1816@TK2MSFTNGP09.phx.gbl...> > I have seen the following behavior: when issuing a Page.FindControl()FindControl()> a> in> > 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> cells> > each grid cell to find the ones I need, especially since finding those> > 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,>> > 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



Reply With Quote

