Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindControl() returns null some times in ItemDataBound and Edit events

Status
Not open for further replies.

jgbeldock

Programmer
Joined
Aug 7, 2003
Messages
1
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 Edit() handler (but it gives the same errors in an 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

 
you can wrap the works in this...
if (e.Item.ItemType == ListItemType.EditItem)
{
your code
}

and ListItemType.Item for your not working with the edit

HTH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top