我有一个简单的页面,有1个PlaceHolder和2个按钮(“添加行”,“获取值”),没有别的。当我点击添加行按钮时,我创建了一个动态表,并在其中放置一些控件的单元格设置ClientIDMoode = static:
private void CreateTableWithSession(bool FirstLoad)
{
Table tbl;
int Row;
if (Session["tbl"]==null)
{
tbl = new Table();
}
else
{
tbl = (Table)Session["tbl"];
}
if (FirstLoad == true)
{
tbl.Rows.Clear();
}
//////////////////////////////////////////////////
if (Session["Row"] == null)
{
Row = 0;
}
else
{
Row = int.Parse(Session["Row"].ToString());
}
if (FirstLoad == true)
{
tbl.Rows.Clear();
}
//////////////////////////////////////////////////
TableRow tr1 = new TableRow();
TableCell tc = new TableCell();
TextBox txtBoxUserName = new TextBox();
txtBoxUserName.ClientIDMode = System.Web.UI.ClientIDMode.Static;
txtBoxUserName.Text = "0";
Col++;
txtBoxUserName.ID = "RowNo" + Row.ToString() + "ColumnNo" + Col.ToString();
tc.Controls.Add(txtBoxUserName);
tc.Width = 200;
tc.VerticalAlign = VerticalAlign.Middle;
tc.HorizontalAlign = HorizontalAlign.Center;
tr1.Cells.Add(tc);
tc = new TableCell();
TextBox txtBoxPassword = new TextBox();
txtBoxPassword.ClientIDMode = System.Web.UI.ClientIDMode.Static;
txtBoxPassword.Text = "0";
Col++;
txtBoxPassword.ID = "RowNo" + Row.ToString() + "ColumnNo" + Col.ToString();
tc.Controls.Add(txtBoxPassword);
tc.Width = 200;
tc.VerticalAlign = VerticalAlign.Middle;
tc.HorizontalAlign = HorizontalAlign.Center;
tr1.Cells.Add(tc);
tbl.Rows.Add(tr1);
Session["tbl"] = tbl;
PlaceHolder1.Controls.Add(tbl);
}
我希望当用户点击“获取价值”按钮时,获得2个文本框值(动态添加)。但每次返回Null。
TextBox txtBoxUserName = this.FindControl("RowNo1ColumnNo1") as TextBox;
我如何修复此问题并访问文本框值?
感谢
答案 0 :(得分:1)
在您可以访问之前,应该重新创建动态创建的控件,并且当您点击Get Value
按钮时,我确定您没有创建控件。有些东西会对你有用......
protected void AddRow_Click(object sender, EventArgs e)
{
CreateTableWithSession()
...................
}
protected void GetValue_Click(object sender, EventArgs e)
{
CreateTableWithSession()
TextBox txtBoxUserName = this.FindControl("RowNo1ColumnNo1") as TextBox;
........................
}