how to access control that dynamically added to page with static ClientIDMode
I have a simple page that hase 1 PlaceHolder and 2 Button("add row","get value") and nothing else.when I click on add row button I create a dynamic Table and place some control in it's cell with set 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++;
txtBoxUserN开发者_如何转开发ame.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);
}
and I want when user clicks on "get value" button ,get 2 textbox values(that dynamically added).but every time it returns Null.
TextBox txtBoxUserName = this.FindControl("RowNo1ColumnNo1") as TextBox;
how I can fix this and access to textbox values?
thanks
Dynamically created controls should be recreated before you can access and I am sure you are not creating the controls when you hit Get Value
button. Something will work for you...
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;
........................
}
精彩评论