public partial class Table_Traversing : System.Web.UI.Page
{
Table table1 = new Table();
TableRow table_row1 = new TableRow();
TableCell table_cell1 = new TableCell();
TableCell table_cell2 = new TableCell();
Label The_text = new Label();
CheckBox checkmate = new CheckBox();
Button button1 = new Button();
public void Page_Init(object sender, EventArgs e)
{
//Table asdasd = new Table();
}
protected void Page_Load(object sender, EventArgs e)
{
The_text.Text = "This is the text :-)";
checkmate.Checked = false;
table_cell2.Controls.Add(checkmate);
table_cell1.Controls.Add(The_text);
table_row1.Controls.AddAt(0, table_cell1);
table_row1.Controls.AddAt(1, table_cell2);
table1.Rows.Add(table_row1);
button1.Text = "click me to export the value";
form1.Controls.AddAt(0, table1);
form1.Controls.AddAt(0, button1);
CheckBox check_or_not = new CheckBox();
che开发者_运维问答ck_or_not = (CheckBox)table1.FindControl("checkmate");
Response.Write(check_or_not.Checked.ToString());
button1.Click += new EventHandler(button1_Click);
}
The error is
Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 59: check_or_not = (CheckBox) table1.FindControl("checkmate");
Line 60:
Line 61: Response.Write(check_or_not.Checked.ToString());
Line 62:
You have to give the control an ID, if you want to use FindControl ... so you have to add this before invoking FindControl:
checkmate.ID = "checkmate";
精彩评论