如何使用特定方案将行添加到表 [英] how to add row to table with specific scheme

查看:64
本文介绍了如何使用特定方案将行添加到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含此div的asp页,此表默认包含两行:

Hi, i have an asp page that contains this div, this table by default contains two rows:

<div>
        <table border="1"  runat="server" id="tblInfo">
            <tr>
                <td>Row 1</td>
            </tr>
            <tr>
                <td>Row 2</td>
            </tr>
        </table>
        <asp:Button runat="server" ID="btnAdd" OnClick="AddNewBtnClick" Text="Add Row"/>
    </div>



这是此页后面的代码:



and this is the code behind of this page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace OTED
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public static int counter = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            rebuildMyTable();
        }

        protected void rebuildMyTable()
        {
            if (this.ViewState["TableRowCount"] == null)
                this.ViewState["TableRowCount"] = 0;
            for (int i = 0; i < Int32.Parse(this.ViewState["TableRowCount"].ToString()); i++)
                AddNewRow();
        }
        protected void AddNewBtnClick(object sender, EventArgs e)
        {
            AddNewRow();
            if (this.ViewState["TableRowCount"] == null)
                this.ViewState["TableRowCount"] = 1;
            else
            {
                this.ViewState["TableRowCount"] = Int32.Parse((this.ViewState["TableRowCount"].ToString())) + 1;
            }
        }
        protected void AddNewRow()
        {
            HtmlTableCell cell = new HtmlTableCell();
            HtmlTableRow row = new HtmlTableRow();
            TextBox txt = new TextBox();
            txt.ID = counter.ToString();
            cell.Controls.Add(txt);
            row.Controls.Add(cell);
            int abc = tblInfo.Rows.Count;
            tblInfo.Controls.AddAt(abc, row);
            counter = counter + 1;
        }

    }
}



最后,我想这样做,当用户单击"btnAdd"时,将每次单击添加一行到表中,但是此代码无法正常工作,请问每个人都可以帮助我吗?
我是ASP入门TNX.



finaly i''m want to do this, when user clicked on "btnAdd", add the one row per click to the table, but this code doesn''t work properly, can every one help me?
i''m a ASP Beginner, TNX.

推荐答案

更改
int abc = tblInfo.Rows.Count;
            tblInfo.Controls.AddAt(abc, row);




To

tblInfo.Rows.Add(row);



您需要添加行,因为控件已经添加到行中,因此无需将控件添加到表中.



You need to add the row, because the controls are already added to the row so no need to add controls to the table.


这篇关于如何使用特定方案将行添加到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆