在动态创建的GridView中访问TextBox的内容

问题描述:

我创建了gridview的动态, 它由文本框和buttonfield组成,在gridview的行命令事件上,我想访问文本框的内容。 我不能够访问该文本框 这里是我的代码在动态创建的GridView中访问TextBox的内容

protected void FillGridListTollCollection() 
    { 
     TollCollectionBLLC tollcollectionBLLC = new TollCollectionBLLC(); 
     dataTable = tollcollectionBLLC.GetTollCollectionDetailsBLLC(187, 1, 1, "10/10/2011"); 

     //put the gridview into the placeholder to get the values of textboxes in gridview 
     PlaceHolder1.Controls.Add(GridListTollColletion); 


     foreach (DataColumn col in dataTable.Columns) 
     { 

      if (col.ToString() == "TollCollID") 
      { 
       BoundField bField = new BoundField(); 
       bField.HeaderText = "TollCollID"; 
       bField.DataField = "TollCollID"; 
       bField.Visible = false; 
       GridListTollColletion.Columns.Add(bField); 
       continue; 
      } 
      if (col.ToString() == "TollAmtApplicable") 
      { 
       BoundField bField = new BoundField(); 
       bField.HeaderText = "TollAmtApplicable"; 
       bField.DataField = "TollAmtApplicable"; 
       bField.Visible = false; 
       GridListTollColletion.Columns.Add(bField); 
       continue; 
      } 
      if (col.ToString() == "TollCollDesc") 
      { 
       BoundField bField = new BoundField(); 
       bField.HeaderText = "Transaction Types"; 
       bField.DataField = "TollCollDesc"; 
       GridListTollColletion.Columns.Add(bField); 
       continue; 
      } 

      if (col.ToString() == "Total") 
      { 
       BoundField bField = new BoundField(); 
       bField.HeaderText = "Total"; 
       bField.DataField = "Total"; 
       GridListTollColletion.Columns.Add(bField); 
       continue; 
      } 

      if (col.ToString().Contains("Rate")) 
      { 
       BoundField bField = new BoundField(); 
       bField.HeaderText = col.ToString(); 
       bField.DataField = col.ToString(); 
       bField.Visible = false; 
       GridListTollColletion.Columns.Add(bField); 
       continue; 
      } 

      //Declare the bound field and allocate memory for the bound field. 
      TemplateField tfield = new TemplateField(); 

      //Initalize the DataField value. 
      tfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName); 
      tfield.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
      tfield.HeaderStyle.VerticalAlign = VerticalAlign.Middle; 
      tfield.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
      tfield.ItemStyle.VerticalAlign = VerticalAlign.Middle; 

      //Initialize the HeaderText field value. 
      tfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName); 

      //Add the newly created bound field to the GridView. 
      GridListTollColletion.Columns.Add(tfield); 

     } 


    protected void GridListTollColletion_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     int tempCarCount = 0; 
// here i am getting exception 
     int.TryParse(((TextBox)GridListTollColletion.Rows[Convert.ToInt32(e.CommandArgument)].Cells[Convert.ToInt32(e.CommandArgument)].Controls[3]).Text , out tempCarCount); 
} 

公共类GridViewTemplate:了Itemplate {// 一个变量来保存ListItemType的类型。 ListItemType _templateType;

//A variable to hold the column name. 
string _columnName; 

//Constructor where we define the template type and column name. 
public GridViewTemplate(ListItemType type, string colname) 
{ 
    //Stores the template type. 
    _templateType = type; 

    //Stores the column name. 
    _columnName = colname; 
} 

void ITemplate.InstantiateIn(System.Web.UI.Control container) 
{ 
    switch (_templateType) 
    { 
     case ListItemType.Header: 
      //Creates a new label control and add it to the container. 
      Label lbl = new Label();   //Allocates the new label object. 
      lbl.Text = _columnName;    //Assigns the name of the column in the lable. 

      container.Controls.Add(lbl);  //Adds the newly created label control to the container. 
      break; 

     case ListItemType.Item: 
      //Creates a new text box control and add it to the container. 
      TextBox tb1 = new TextBox();       //Allocates the new text box object. 
       tb1.Width = 60; 
       tb1.Height = 22; 
       tb1.MaxLength = 50; 
       tb1.ID = "v"; 
       //Creates a column with size 4. 
       tb1.DataBinding += new EventHandler(tb1_DataBinding); //Attaches the data binding event. 
       tb1.Columns = 7; 
      container.Controls.Add(tb1); 

      //Adds the newly created textbox to the container. 
      break; 

     case ListItemType.EditItem: 
      //As, I am not using any EditItem, I didnot added any code here. 
      break; 

     case ListItemType.Footer: 
      CheckBox chkColumn = new CheckBox(); 
      chkColumn.ID = "Chk" + _columnName; 
      container.Controls.Add(chkColumn); 
      break; 
    } 
} 

    public IOrderedDictionary ExtractValues(Control container) 
    { 
     OrderedDictionary dict = new OrderedDictionary(); 
     if (_templateType == ListItemType.Item) 
     { string value = ((TextBox)container.FindControl("tb1" + _columnName)).Text; 
      dict.Add(_columnName, value); } 
     return dict; 
    } 

    void tb1_DataBinding(object sender, EventArgs e) 
    { 
     TextBox txtdata = (TextBox)sender; 
     GridViewRow container = (GridViewRow)txtdata.NamingContainer; 
     object dataValue = DataBinder.Eval(container.DataItem, _columnName); 
     if (dataValue != DBNull.Value) 
     { 
      txtdata.Text = dataValue.ToString(); 
     } 
    } 



    public GridViewTemplate() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 

} 

该模板是静态应用的。所以,当你试图去做的时候,你将无法访问控件。

尝试使用 -

var textbox = GridListTollColletion.FindControl(<yourtextbox>); 
+0

谢谢,但findcontrol方法需要的文本框的ID,我没有因为创建它们@运行时 –

+0

你可以在数据绑定时设置文本框的ID属性。 – pavanred

+0

我已经设置它,如果你看到我的代码 –

岂不是方便,如果你绑定的GridView.RowDataBound事件这些添​​加的控件。
检查这个BindData to ITemplate for GridView in code behind

如果你正在跟踪这个MSDN文章How To: Create ASP.NET Web Server Control Templates Dynamically采取的想法这个再查这个代码片断。

static void Item_DataBinding(object sender, System.EventArgs e) 
{ 
    PlaceHolder ph = (PlaceHolder)sender; 
    RepeaterItem ri = (RepeaterItem)ph.NamingContainer; 
    Int32 item1Value = (Int32)DataBinder.Eval(ri.DataItem, "CategoryID"); 
    String item2Value = (String)DataBinder.Eval(ri.DataItem, "CategoryName"); 
    ((Label)ph.FindControl("item1")).Text = item1Value.ToString(); 
    ((Label)ph.FindControl("item2")).Text = item2Value; 
} 

它已经Item_DataBinding一个static方法。你正在静态地设置这个东西,所以查阅这篇文章来改进你的代码。

在编辑模板我建议你使用RowBound事件来处理这些模板控件..在那里你可以很容易地访问它们。

+0

谢谢,现在我正在访问所有来自Request.Param [“”]的文本框我在行中获取所有值 –