0

我有一种方法可以在每次单击按钮时创建文本。但是我正在尝试以另一种方法访问ID。有些东西似乎超出了范围,这里是代码:Textbox

    public partial class _Default : System.Web.UI.Page
{
    TextBox box = new TextBox();

    protected void Page_Load(object sender, EventArgs e)
    {



    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
        Label1.Text = ViewState["count"].ToString();
        int test = int.Parse(string.Format("{0}", ViewState["count"]));

            for (int j = 0; j < test; j++)
            {

                TableRow r = new TableRow();

                box.ID = "Textbox" + j;

                TableCell c = new TableCell();
                c.Controls.Add(box);
                r.Cells.Add(c);
                table1.Rows.Add(r);
                Response.Write(box.ID);

            }

            if (test == 4)
            {
                Button1.Visible = false;
            }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        Response.Write(box.ID);
    }

我想Button2_Click打印出来box.ID以确保它能够访问它。

4

4 回答 4

3

您的Button1_ClickButton2_Click事件将在两个单独的回发中。 Box在这种情况下,除了在默认构造函数中设置的任何值之外,不会为其分配任何值。

编辑: 因为您在回发时丢失了所有代码隐藏对象和值的状态。

于 2013-07-11T19:28:29.780 回答
2

要始终创建与用户单击次数一样多的文本框Button1Page_Load您必须运行代码:

int test = 0;
if (ViewState["count"] != null)
   test = (int)ViewState["count"];

for (int j = 0; j < test; j++)
{
    TextBox box = new TextBox();
    box.ID = "Textbox" + j;
    textBoxes.Add(box.ID, box);
    TableRow r = new TableRow();
    TableCell c = new TableCell();
    c.Controls.Add(box);
    r.Cells.Add(c);
    table1.Rows.Add(r);
}

textBoxesPage成员在哪里:

Dictionary<string, TextBox> textBoxes = new Dictionary<string, TextBox>();

然后,在Button2_Click

string ids = "";
foreach(string id in textBoxes.Keys)
{
    ids = ids + id + ",";
}
Response.Write(ids);
于 2013-07-11T20:00:51.160 回答
2

关键是您需要在回发时重新加载动态创建的控件。

否则,它们将变为 null,并且您无法访问这些值。

注意:您不需要使用Response.Write.

这是示例。

<asp:Label runat="server" ID="Label1"></asp:Label>
<asp:Table runat="server" ID="table1"></asp:Table>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" 
   Text="Create TextBoxes" />
<asp:Button runat="server" ID="Button2" OnClick="Button2_Click" 
  Text="Submit" />

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        // Reload those control back. It can be in either init or load event.
        int total = Count;
        Label1.Text = total.ToString();

        for (int i = 0; i < total; i++)
            CreateTextBoxes(i);

        Count = total;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    Count = Count++;
    CreateTextBoxes(Count);

    if (Count == 4)
    {
        Button1.Visible = false;
    }
}

protected void Button2_Click(object sender, EventArgs e)
{
    var results = new List<string>();
    foreach (TableRow row in table1.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            var textBox = cell.Controls[0] as TextBox;
            if (textBox != null)
            {
                results.Add(textBox.Text);
            }
        }
    }
    Label1.Text += string.Join(",", results);
}

private int Count
{
    get { return Convert.ToInt32(ViewState["count"] ?? "0"); }
    set { ViewState["count"] = value; }
}

private void CreateTextBox(int j)
{
    var box = new TextBox();
    box.ID = "Textbox" + j;
    box.Text = "Textbox" + j;

    var c = new TableCell();
    c.Controls.Add(box);

    var r = new TableRow();
    r.Cells.Add(c);

    table1.Rows.Add(r);
}

输出

在此处输入图像描述

于 2013-07-11T20:02:07.747 回答
0

使用asp.net的工作方式,您将box在每次回发时重新创建(单击Button1时和单击时Button2)。因此,当您单击时Button2,即使您已经运行了 for 的代码Button1,您也会得到一个全新的box.

Button2中,您需要做一些事情来textbox从您添加它的页面本身访问 。我不确定如何处理你正在做的事情,但类似

var mytextBox = table1.Rows[x].Cells[y].Controls[z] as TextBox; // PSEUDOCODE
于 2013-07-11T19:29:18.390 回答