丢失动态创建的文本框值

时间:2013-06-25 06:21:58

标签: c# asp.net

请参阅以下代码:

    protected void btnAddField_click( Object sender, EventArgs e ) {
        int FieldCount = 0;
        if (ViewState["FieldCount"] != null)
        {
            FieldCount = (int)ViewState["FieldCount"];
        }

        Table tbl = new Table();
        if (Session["DynamicTable"] != null)
        {
            tbl = (Table)Session["DynamicTable"];
        }

        CheckBox chkNewField = new CheckBox();
        chkNewField.ID = "chkNewField" + FieldCount.ToString();
        chkNewField.Checked = true;

        Label LblNewLabel = new Label();
        LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
        LblNewLabel.Text = "New Lable";

        TextBox TxtNewLabel = new TextBox();
        TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

        Label LblNewValue = new Label();
        LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
        LblNewValue.Text = "New Value";

        TextBox TxtNewValue = new TextBox();
        TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

        TableRow tRow = new TableRow();

        TableCell tCell1 = new TableCell();
        TableCell tCell2 = new TableCell();
        tCell2.Attributes.Add("class", "medium");
        TableCell tCell3 = new TableCell();
        tCell3.Attributes.Add("class", "medium");
        TableCell tCell4 = new TableCell();
        TableCell tCell5 = new TableCell();
        tCell5.Attributes.Add("class", "medium");
        TableCell tCell6 = new TableCell();
        tCell6.Attributes.Add("class", "medium");

        tCell1.Controls.Add(chkNewField);
        tCell2.Controls.Add(LblNewLabel);
        tCell3.Controls.Add(TxtNewLabel);
        tCell4.Controls.Add(new LiteralControl(""));
        tCell5.Controls.Add(LblNewValue);
        tCell6.Controls.Add(TxtNewValue);

        tRow.Cells.Add(tCell1);
        tRow.Cells.Add(tCell2);
        tRow.Cells.Add(tCell3);
        tRow.Cells.Add(tCell4);
        tRow.Cells.Add(tCell5);
        tRow.Cells.Add(tCell6);

        tbl.Rows.Add(tRow);
        placeHolderTable.Controls.Remove(tbl);
        placeHolderTable.Controls.Add(tbl);
        Session["DynamicTable"] = tbl;
        FieldCount++;
        ViewState["FieldCount"] = FieldCount;
}

protected void BtnPublish_click( object sender, EventArgs e ) { 
   TextBox tb = (TextBox)placeHolderTable .FindControl( "TxtNewLabel1" ); 
}

动态添加字段工作正常。但

  1. 我在每个帖子后面的文本框中输入的值
  2. 我无法从文本框中获取值。
  3. 请帮帮我。 提前致谢, 马努

2 个答案:

答案 0 :(得分:1)

关于动态创建控件的简单规则是,如果要接收任何值,则必须在Init上重新创建它们。

参考:Page life cycle

如果在此之前未创建控件,则会在函数ProcessPostData中处理回发数据,并且不会分配用户输入。

我会将控件创建放入一个方法中,跟踪在会话(或ViewState)中添加动态控件,然后在每个Init之后添加它们,直到我想要页面上的控件。

答案 1 :(得分:1)

虽然有一种棘手的方法可以在PostBack中获取textBoxes的值。 您可以在下面使用此代码。

private string GetValue(string ControlID)
{
     string[] keys = Request.Form.AllKeys;
     string value = string.Empty;
     foreach (string key in keys)
     {
         if (key.IndexOf(ControlID) >= 0)
         {
             value = Request.Form[key].ToString();
             break;
         }
     }

     return value;
}

然后在PostBack中使用此方法。

protected void BtnPublish_click( object sender, EventArgs e ) 
{ 
    string TxtNewLabel1Val = GetValue("TxtNewLabel1");
}