如何动态地添加表行 [英] How to Dynamically add Table rows

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

问题描述

我试图在表中的行按钮即可添加值。它的工作数据库,但不工作的网页上。它覆盖在最后一排页面上。

I am trying to add values in table rows on button click. It's working on database but not working on web page. It override on last row on page.

怎样才能在每一个按钮点击新行。

How can I generate new row on every button click.

这是我的按钮单击code -

here is my button click code--

protected void Page_Load(object sender, EventArgs e)
{
    tblAdd.Visible = false;
    Label1.Visible = false;

    //Label2.Visible = false;


}

protected void btnSave_Click(object sender, EventArgs e)
{
    int count = 1;
    if (Page.IsValid)
    {

        TableRow NewRow1 = new TableRow();

        //1st cell
        TableCell NewCell1 = new TableCell();

        //new checkbox
        CheckBox newCheckBox1 = new CheckBox();


        // adding lebel into cell
        NewCell1.Controls.Add(newCheckBox1);

        // adding cells to row
        NewRow1.Cells.Add(NewCell1);

        //2ed cell
        TableCell NewCell2 = new TableCell();

        Label newLabel1 = new Label();
        count = count + 1;
        newLabel1.Text = txtName.Text;
        newLabel1.ID = "label" + count;

        NewCell2.Controls.Add(newLabel1);
        NewRow1.Cells.Add(NewCell2);

        //adding row into table
        tblLanguages.Rows.Add(NewRow1);

        btnAdd.Visible = true;
        btnDelete.Visible = true;
        Label2.Visible = true;
        Label2.Text = "Successfully Added";
        add();
    }
    txtName.Text = "";
}

public int add()
{
    string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
    SqlConnection sqlConnection = new SqlConnection(strcon);

    SqlCommand command = new SqlCommand("hrm_AddLanguages", sqlConnection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
    command.Parameters.Add("@CreatedOn", SqlDbType.DateTime).Value = DateTime.Now;
    command.Parameters.Add("@UpdatedOn", SqlDbType.DateTime).Value = DateTime.Now;
    command.Parameters.Add("@CreatedBy", SqlDbType.BigInt).Value = 1;
    command.Parameters.Add("@UpdatedBy", SqlDbType.BigInt).Value = 1;
    command.Parameters.Add("@IsDeleted", SqlDbType.Bit).Value = 0;
    sqlConnection.Open();
    return command.ExecuteNonQuery();
}

请帮我。

推荐答案

如果有可能尽量做到这一点使用网格视图。这是更好地利用网格视图,而不是表。

If possible try to do this using Grid view. It is better to use grid view instead of table.

看看下面的链接。这将帮助你找到你的解决方案。

Check out below link. It will help you to find your solution.

<一个href=\"http://stackoverflow.com/questions/23360564/datatable-in-asp-net-session/23361055#23361055\">DataTable在ASP.Net对话

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

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