2008/09/30

Tips: Get the GridView control from the TextBox insides the GridView (7/21/09 updated)

Scenario:
1.You have one or more than one GridView(s) on your page.
2.Each GridView has one or more TextBox(s) in it.
3.At the TextChanged() event, you want to/have to know which GridView owns the event.
Here is the way to get the parent control of that TextBox:
//get the parent gridview
GridViewRow gvr = (GridViewRow)(((TextBox)sender).Parent).Parent;
GridView gv = (GridView)(gvr.Parent).Parent;

Once you get the GridViewRow, you can get the RowIndex value. Once you get the GridView, you get everything...

7/21/09 updated:
A better way to get the same thing by using NamingContainer property:
GridViewRow gvr = (GridViewRow)((TextBox)sender).NamingContainer;
GridView gv = (GridView)gvr.NamingContainer;

No comments:

Post a Comment