2013-07-16 55 views
2

我有一个数据库的列中包含多个值用逗号分隔 现在我想编辑它,所以我检索从数据库分离它的价值,并将其存储在字符串数组 然后生成文本框,并指定值文本框,现在我想从生成文本框 这里获取更新的值是代码检索在asp.net中动态生成的文本框的值

static string[] temp; 
    static string[] temp1; 
    static TextBox tbin; 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
      { 
       barcode_lab.Text = GridView1.SelectedRow.Cells[1].Text; 
       date_lab.Text = GridView1.SelectedRow.Cells[2].Text; 
       string tin = GridView1.SelectedRow.Cells[3].Text; 
       string tout = GridView1.SelectedRow.Cells[4].Text; 

       //////////////conversion///////////////////// 

       temp = tin.Split(','); 
       for (int i = 0; i < temp.Length; i++) 
       { 
        tbin = new TextBox(); 
        tbin.Text = temp[i]; 
        tbin.ID = "timein"+i; 
        PlaceHolder6.Controls.Add(tbin); 
        PlaceHolder6.Controls.Add(new LiteralControl("<br />")); 
       } 
      } 

更新:

protected void update_btn_Click(object sender, EventArgs e) 
    { 

      foreach (GridViewRow row in GridView1.Rows) 
      { 
       foreach (TableCell cell in row.Cells) 
       { 
        List<TextBox> textBoxes = MissingExtention.GetAllControls(cell).Where(c => c is TextBox); 

       } 
      } 
    } 

public static class MissingExtention 
{ 
    public static List<Control> FlattenChildren(this Control control) 
    { 
     var children = control.Controls.Cast<Control>(); 
     return children.SelectMany(c => FlattenChildren(c).Where(a => a is TextBox)).Concat(children).ToList(); 
    } 

    public static List<Control> GetAllControls(Control control) 
    { 
     var children = control.Controls.Cast<Control>(); 
     return children.SelectMany(c => FlattenChildren(c)).Concat(children).ToList(); 
    } 
} 

现在出现以下错误:

  1. foreach语句无法在类型“System.Web.UI.WebControls.GridView”的变量操作,因为“System.Web.UI.WebControls.GridView”不包含公共定义为“的GetEnumerator”
  2. 最好重载方法用于匹配“GPServices.MissingExtention.GetAllControls(System.Web.UI.Control)”具有一些无效参数
  3. 参数1:不能从“的System.Web.UI转换。 ControlCollection'改为'System.Web.UI.Control'

回答

0

试试这个:

新类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

/// <summary> 
/// Summary description for OhterMethods 
/// </summary> 
public static class OhterMethods 
{ 
    public static List<Control> FlattenChildren(this Control control) 
    { 
     var children = control.Controls.Cast<Control>(); 
     return children.SelectMany(c => FlattenChildren(c).Where(a => a is TextBox ||  a is Label || a is Literal || a is Button || a is GridView || a is HyperLink || a  is DropDownList)).Concat(children).ToList(); 
    } 

    public static List<Control> GetAllControls(Control control) 
    { 
     var children = control.Controls.Cast<Control>(); 
     return children.SelectMany(c => FlattenChildren(c)).Concat(children).ToList(); 
    } 
} 

获取所有的文本框

foreach (GridViewRow row in GridView1.Rows) 
{ 
    foreach (TableCell cell in row.Cells) 
    { 
     List<TextBox> textBoxes = OtherMethods.GetAllControls(cell).Where(c => c is TextBox); 
    } 
} 

您还可以通过ID获取特定的文本框:

OtherMethods.GetAllControls(细胞).FirstOrDefault(c => c是TextBox & & c.ID ==“txtTex tBox“)

你必须得到gridview的每一行。之后,对于特定的列或其中的每一个,获取所有文本框并提取值。您必须以该列作为参数调用上述方法。如果必须按id更新特定行,则可以向每个文本框添加一个属性,或者向每行的列添加隐藏字段并提取该值。

1

确定了最简单的方式来做到这一点

for (int i = 0; i < temp.Length; i++) 
    { 
     PlaceHolder6.Controls.Add(new LiteralControl("<input id='txt' name='txtName' type='text' value='"+temp[i]+"' />")); 
     PlaceHolder6.Controls.Add(new LiteralControl("<br />")); 
    } 

protected void update_btn_Click(object sender, EventArgs e) 
    { 
     Label1.Text = Request.Form["txtName"]; 
    } 

也回到了我在一个字符串文本框中的所有值,这也有助于我

相关问题