Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > FindControl() returns NULL when object exists in Template?

Reply
Thread Tools

FindControl() returns NULL when object exists in Template?

 
 
James G. Beldock
Guest
Posts: n/a
 
      08-07-2003
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


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      08-07-2003
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
http://www.takempis.com
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:(E-Mail Removed)...
> 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:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Begin() applied on empty Vector returns NULL or non null value???? Col C++ 1 04-21-2006 01:12 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM



Advertisments