1

我写了一个小程序,它在单击按钮后的每一行中创建三个文本框。每次单击都会调用使用文本框createControl创建的方法rows[0..n-1],然后在方法中Button_Click添加row[n]. 因此,例如,当我有 1 行或更多行带有带有一些文本的文本框然后单击按钮时,回发后我会遇到文本框中信息的奇怪情况:row(n-1)只有第一个文本框中有文本,而其他 2 行没有.

例如,我有 3 行文本:

1 1 1
2 2 2
3 3 3

然后点击添加按钮:

(1)       (1)     (1)
(2)       (2)     (2)
(3,3,3)   (empty) (empty)

和新的空行
所以,在(n-1)第一个文本框中的行中我得到三个值,为什么?

    TextBox[] textbox;
    TextBox[] textbox2;
    TextBox[] textbox3;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            createControls();
        }
        else
        {
            Num = 0;
        }
    }

    private int Num
    {
        get { return (int)ViewState["num"]; }
        set { ViewState["num"] = value; }
    }

    private void createControls()
    {
        textbox = new TextBox[Num + 1];
        textbox2 = new TextBox[Num + 1];
        textbox3 = new TextBox[Num + 1];
        for (int i = 0; i < Num; i++)
        {
            textbox[i] = new TextBox();
            textbox[i].ID = "textbox[" + i + "]";
            PlaceHolder1.Controls.Add(textbox[i]);
            textbox2[i] = new TextBox();
            textbox2[i].ID = "textbox2[" + i + "]";
            PlaceHolder1.Controls.Add(textbox2[i]);
            textbox3[i] = new TextBox();
            textbox3[i].ID = "textbox3[" + i + "]";
            PlaceHolder1.Controls.Add(textbox3[i]);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        textbox[Num] = new TextBox();
        textbox[Num].ID = "textbox[" + Num + "]";
        PlaceHolder1.Controls.Add(textbox[Num]);
        textbox2[Num] = new TextBox();
        textbox2[Num].ID = "textbox[" + Num + "]";
        PlaceHolder1.Controls.Add(textbox2[Num]);
        textbox3[Num] = new TextBox();
        textbox3[Num].ID = "textbox[" + Num + "]";
        PlaceHolder1.Controls.Add(textbox3[Num]);
        Num++;
    }
4

1 回答 1

1

您正在 Button1_Click 中创建具有相同 ID 的文本框。

protected void Button1_Click(object sender, EventArgs e)
{
    textbox[Num] = new TextBox();
    textbox[Num].ID = "textbox[" + Num + "]";
    PlaceHolder1.Controls.Add(textbox[Num]);
    textbox2[Num] = new TextBox();
    textbox2[Num].ID = "textbox[" + Num + "]"; // Must be "textbox2[" + Num + "]"
    PlaceHolder1.Controls.Add(textbox2[Num]);
    textbox3[Num] = new TextBox();
    textbox3[Num].ID = "textbox[" + Num + "]"; // Must be "textbox3[" + Num + "]"
    PlaceHolder1.Controls.Add(textbox3[Num]);
    Num++;
}

更新:

实际上,您不需要 TextBox 数组。

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

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        for (int i = 0; i < Num; i++)
            CreateTextBoxes(i);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    CreateTextBoxes(Num++);
}

protected void CreateTextBoxes(int id)
{
    PlaceHolder1.Controls.Add(new TextBox {ID = "textbox1" + id});

    PlaceHolder1.Controls.Add(new TextBox {ID = "textbox2" + id});

    PlaceHolder1.Controls.Add(new TextBox {ID = "textbox3" + id});
}
于 2013-09-17T22:09:20.280 回答