在gridview中查找动态templatefield控件时遇到问题

问题描述:


我有一个gridview,我在代码隐藏中添加了一个templatefield

Hi,
I have a gridview where I in codebehind add a templatefield

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



我的问题是当我尝试更新时找不到控件.

这是我的EditItemTemplate



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

Here is my EditItemTemplate

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

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



谁能指导我找到结果"控件.

在此先感谢
Morten



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

Thanks in advance
Morten

我认为您的代码有问题
I think problem in your code
protected void OnUpdate(object sender, GridViewUpdateEventArgs e)
        {
            object res = GridView1.Rows[GridView1.EditIndex].FindControl("Result");
        }


应该是


It should be

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
        }


有关更多信息,请参见以下内容.

在Asp.Net GridView控件中简单地插入,选择,编辑,更新和删除 [


For more information refer the below one.

Simple Insert, Select, Edit, Update and Delete in Asp.Net GridView control[^]


感谢您的回复,

我仍然得到一个空对象.

在gridview中,还有另一个模板字段名称",它由两个边界字段(名字"和姓氏")组成
此列是在设计阶段添加的,而不是在后面的代码中添加的.
使用标记中添加的ID,我可以轻松找到这些控件
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
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