Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何动态添加表行_C#_Asp.net_Webforms - Fatal编程技术网

C# 如何动态添加表行

C# 如何动态添加表行,c#,asp.net,webforms,C#,Asp.net,Webforms,我试图在单击按钮时在表行中添加值。它在数据库上工作,但在网页上不工作。它覆盖在第页的最后一行 如何在每次单击按钮时生成新行 这是我的按钮点击代码-- 请帮助我。如果可能,请尝试使用网格视图执行此操作。最好使用网格视图而不是表 查看下面的链接。它将帮助您找到解决方案 protected void Page_Load(object sender, EventArgs e) { tblAdd.Visible = false; Label1.Visible = false;

我试图在单击按钮时在表行中添加值。它在数据库上工作,但在网页上不工作。它覆盖在第页的最后一行

如何在每次单击按钮时生成新行

这是我的按钮点击代码--


请帮助我。

如果可能,请尝试使用网格视图执行此操作。最好使用网格视图而不是表

查看下面的链接。它将帮助您找到解决方案

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