在回发之后发生了一些奇怪的文本框

时间:2013-09-17 21:13:11

标签: asp.net postback

我写了一个小程序,在点击按钮后每行创建三个textBox。每次点击调用方法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++;
    }

1 个答案:

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