页面加载时会自动调用Page_Load事件,而table的onload事件又再次调用了Page_Load,所以就生成了10行。
这跟 if(!IsPostBack){}没有关系,但是还是加上的好:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { for (int i = 0; i < 5; i++) { HtmlTableRow r = new HtmlTableRow(); r.BgColor = "red"; for (int j = 0; j < 4; j++) { HtmlTableCell c = new HtmlTableCell(); c.InnerText = i.ToString(); r.Cells.Add(c); } table1.Rows.Add(r); } } }
希望能帮到你,还望采纳
在这里加上if(!IsPostBack){for (int i = 0; i < 5; i++) { HtmlTableRow r = new HtmlTableRow(); r.BgColor = "red"; for (int j = 0; j < 4; j++) { HtmlTableCell c = new HtmlTableCell(); c.InnerText = i.ToString(); r.Cells.Add(c); } table1.Rows.Add(r); } }