Click here to Skip to main content
15,884,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a gridview where I in codebehind add a templatefield
C++
TemplateField resultField = new TemplateField();
  resultField.HeaderTemplate = new ResultIntTemplate DataControlRowType.Header, header);
resultField.EditItemTemplate = new ResultIntEditTemplate(DataControlRowType.DataRow, header);
                               resultField.ItemTemplate = new ResultIntTemplate(DataControlRowType.DataRow, header);


My issue is when I'm trying to update I cannot find the control.

Here is my EditItemTemplate
C++
public class ResultIntEditTemplate : ITemplate
  {
      private DataControlRowType templateType;
      private string columnName;

      public ResultIntEditTemplate(DataControlRowType type, string colname)
      {
          templateType = type;
          columnName = colname;
      }

      public void InstantiateIn(System.Web.UI.Control container)
      {
          TextBox field_txtbox = new TextBox();
          field_txtbox.ID = "Result";
          field_txtbox.Text = String.Empty;
          field_txtbox.DataBinding += new EventHandler(Result_DataBinding);

          container.Controls.Add(field_txtbox);
      }

private void Result_DataBinding(Object sender, EventArgs e)
      {
          object bound_value_obj = null;
          TextBox field_txtbox = (TextBox)sender;
          field_txtbox.Width = 24;

          GridViewRow row = (GridViewRow)field_txtbox.NamingContainer;
                   bound_value_obj = DataBinder.Eval(row.DataItem, "Result");
          if (bound_value_obj == null)
          {
              field_txtbox.Text = String.Empty;
          }
          else
          {
              field_txtbox.Text = bound_value_obj.ToString();
          }
     }
}


In my update command when i try to find the "Result" textbox I get a null object

C#
protected void OnUpdate(object sender, GridViewUpdateEventArgs e)
        {
            object res = GridView1.Rows[GridView1.EditIndex].FindControl("Result");
        }


Could anyone guide me in finding the "Result" control.

Thanks in advance
Morten
Posted

I think problem in your code
C#
protected void OnUpdate(object sender, GridViewUpdateEventArgs e)
        {
            object res = GridView1.Rows[GridView1.EditIndex].FindControl("Result");
        }

It should be
C#
protected void OnUpdate(object sender, GridViewUpdateEventArgs e)
        {
            object res = GridView1.Rows[e.RowIndex].FindControl("Result");
TextBox txtResult = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Result"); //Use casting to get the result as appropriate object. Here you can get Textbox
        }

For more information refer the below one.

Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control[^]
 
Share this answer
 
v2
Thanks for the reply,

I'm still getting a null object.

In the gridview there is another templatefield "Name" that consist of two boundfields("Firstname" and "Lastname")
This column is added in the design phase and not in the codebehind.
I don't have any trouble finding those controls using the ID added in the markup
C++
object firstname = GridView1.Rows[e.RowIndex].FindControl("FirstnameLabel");


My issue is with the result column that I add in codebehind.

Any comments/suggestions are more than welcome :)

Best Morten
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900