2

我在 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'
4

2 回答 2

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"];
    }

它还以单个字符串返回文本框的所有值,这也对我有帮助

于 2013-07-17T04:39:27.580 回答
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 => c is TextBox && c.ID == "txtTextBox")

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

于 2013-07-16T08:18:25.867 回答