GridView FindControl problem

Hi, I build a Custom GridView in code Behine with a TemplateField like this:
TemplateField field = new TemplateField();
            field.SortExpression = dc.ColumnName;
            field.ItemTemplate = new FormattedTemplate(ListItemType.Item, dc);
            field.EditItemTemplate = new FormattedTemplate(ListItemType.EditItem, dc);
in RowDataBound event j can retrieve my control in this way:
....
...
HMDateTextField box = e.Row.FindControl("txtRefDate") as HMDateTextField;

but if J use the same istruction in RowUpdating event:
....
GridViewRow r = this.Rows[e.RowIndex];
HMDateTextField box = r.FindControl("txtRefDate") as HMDateTextField;
box is always null...
thank's in advance
hmadeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaylanreilorCommented:
Who is this in your event handler ? Are you sure that it is your GridView ? To which class is attached your event handler ?
0
Pratima PharandeCommented:
try with Gridview name instade of this

GridViewRow r = Gridview1.Rows[e.RowIndex];
HMDateTextField box = r.FindControl("txtRefDate") as HMDateTextField;
0
hmadeAuthor Commented:
My class is inehrit from gridview in this way:

 public class HMBodyEdit : GridView,INamingContainer
 {.......

in OnLoadMethod I've override:
...
 protected override void OnLoad(EventArgs e)
 {
.........        
base.RowEditing += new GridViewEditEventHandler(HMBodyEdit_RowEditing);
.........        
.........        

                       
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

kaylanreilorCommented:
So as I've said and as pratima_mcs says : why do you use "this" ? (GridViewRow r = this.Rows[e.RowIndex];)
0
hmadeAuthor Commented:
what shoud i use?
J also try with:
 void HMBodyEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow r = ((HMBodyEdit)sender).Rows[e.RowIndex];
            HMDateTextField box = r.FindControl("txtRefDate") as HMDateTextField;
        .....

nothing to do !!    
0
LiFePaTChCommented:
EnableViewState to true in the GridView properties.
0
hmadeAuthor Commented:
Thank's j try but nothing to do !!
If i change my code calling dataBind:
void HMBodyEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
base.DataBind();
GridViewRow r = ((HMBodyEdit)sender).Rows[e.RowIndex];
HMDateTextField box = r.FindControl("txtRefDate") as HMDateTextField;

box is not null, but I don't think this is the right way because i retrieve old textfield value...



0
kaylanreilorCommented:
BTW you write "base.RowEditing += new GridViewEditEventHandler(HMBodyEdit_RowEditing);" in
OnLoad... So I'm just curious of 2 stuffs :
- why not "this.RowEditing += new GridViewEditEventHandler(HMBodyEdit_RowEditing);" (that may explain why you have to write base.DataBind() in your event handler)
- why overriding OnLoad instead of using InitializeComponent(); (but this has nothing to do with your problem)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hmadeAuthor Commented:
I think that this.RowEditing += new GridViewEditEventHandler(HMBodyEdit_RowEditing) is the same
of base.RowEditing += new GridViewEditEventHandler(HMBodyEdit_RowEditing) because my class is inherit from GridView.


0
kaylanreilorCommented:
Except that you don't know you havn't got several collection bindable to data at different level of the inheritance, do you ?
0
hmadeAuthor Commented:
that's right, I change my code but the result is the same.....
0
kaylanreilorCommented:
OK, OK, if you are sure that the problem is not there...
Did you check the value of e.RowIndex ?
0
hmadeAuthor Commented:
yes in my test i'm editing the first row and rowidx is 0
 
0
hmadeAuthor Commented:
In my gridview I've 2 column, the first is a TemplateField with EditItemtemplate, the secondo is a BoundField I see that the problem is present only in first column, so i think that the problem shoud be in EditItemTemplate.
For instance edititemtemplate J use:

 public class FormattedTemplate :ITemplate, INamingContainer
    {
        ListItemType _type;
        DataColumn _dc;
        .........
public void InstantiateIn(System.Web.UI.Control container)
{....
....
case ListItemType.EditItem:
  TextBox dTxt = new TextBox();
                                dTxt.ID = "txt" + _dc.ColumnName;
                                //txt.MaxLength = 10;
                                dTxt.Width = Unit.Pixel(120);
                           
                                container.Controls.Add(dTxt);
break;
what' do you mean?
0
hmadeAuthor Commented:
Ok, I change my code and use onInit for descrive column structure and all song good ...
Thank's a log
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.