简体   繁体   English

如何动态添加表行

[英]How to Dynamically add Table rows

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. 如何在每次单击按钮时生成新行。

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();
}

Please Help me. 请帮我。

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. 这将帮助您找到解决方案。

DataTable in ASP.Net Session ASP.Net会话中的DataTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM