We help IT Professionals succeed at work.

GridView FindControl problem

hmade
hmade asked
on
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
Comment
Watch Question

Who is this in your event handler ? Are you sure that it is your GridView ? To which class is attached your event handler ?
CERTIFIED EXPERT

Commented:
try with Gridview name instade of this

GridViewRow r = Gridview1.Rows[e.RowIndex];
HMDateTextField box = r.FindControl("txtRefDate") as HMDateTextField;

Author

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);
.........        
.........        

                       
So as I've said and as pratima_mcs says : why do you use "this" ? (GridViewRow r = this.Rows[e.RowIndex];)

Author

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 !!    
EnableViewState to true in the GridView properties.

Author

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...



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)

Author

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.


Except that you don't know you havn't got several collection bindable to data at different level of the inheritance, do you ?

Author

Commented:
that's right, I change my code but the result is the same.....
OK, OK, if you are sure that the problem is not there...
Did you check the value of e.RowIndex ?

Author

Commented:
yes in my test i'm editing the first row and rowidx is 0
 

Author

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?

Author

Commented:
Ok, I change my code and use onInit for descrive column structure and all song good ...
Thank's a log

Explore More ContentExplore courses, solutions, and other research materials related to this topic.