【问题标题】:Traversing a table, whats wrong with my code?遍历表,我的代码有什么问题?
【发布时间】:2010-09-04 04:48:14
【问题描述】:
 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();

    check_or_not = (CheckBox)table1.FindControl("checkmate");

    Response.Write(check_or_not.Checked.ToString());


    button1.Click += new EventHandler(button1_Click);





 }

错误是

对象引用未设置为对象的实例。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

来源错误:

第 59 行:check_or_not = (CheckBox) table1.FindControl("checkmate");

第 60 行:

第 61 行:Response.Write(check_or_not.Checked.ToString());

第 62 行:

【问题讨论】:

    标签: c#


    【解决方案1】:

    如果你想使用 FindControl,你必须给控件一个 ID ...所以你必须在调用 FindControl 之前添加它:

    checkmate.ID = "checkmate";
    

    【讨论】:

    • o 我以为控件的名字就是id!!我猜错了:(
    • 能否请您提供参考,解释如何检索表格的各个方面,如行号、单元格编号等。谢谢
    • 也许如果你问另一个问题;)
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多