在asp.net中检索动态生成的Textbox的值

时间:2013-07-16 07:21:34

标签: asp.net webforms

我在db中有一个列,其中包含以逗号分隔的多个值 现在我想编辑它,所以我从db中检索值分开它并将其存储在字符串数组中 然后生成文本框并将值分配给文本框现在我想从生成的文本框中检索更新的值 这是代码

    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'

2 个答案:

答案 0 :(得分: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"];
    }

它还会返回单个字符串中文本框的所有值,这也有助于我

答案 1 :(得分: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(cell).FirstOrDefault(c =&gt; c是TextBox&amp;&amp; c.ID ==“txtTextBox”)

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