C# 在asp.net的asp表中动态添加表行

C# 在asp.net的asp表中动态添加表行,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我试图在asp表中添加表行,但无法解决此问题 我试图在点击按钮时添加新行,但它在点击按钮时生成,但在返回后它消失了。如何绑定asp表使其保留在表中 这是我的密码- Default.aspx <body> <form id="form1" runat="server"> <asp:Table ID="tblAdd" runat="server" BorderWidth="2"> <asp:TableHeaderRow>

我试图在asp表中添加表行,但无法解决此问题

我试图在点击按钮时添加新行,但它在点击按钮时生成,但在返回后它消失了。如何绑定asp表使其保留在表中

这是我的密码-

Default.aspx

<body>
<form id="form1" runat="server">
    <asp:Table ID="tblAdd" runat="server" BorderWidth="2">
        <asp:TableHeaderRow>
            <asp:TableHeaderCell ColumnSpan="2">
                Add Languages
            </asp:TableHeaderCell>
        </asp:TableHeaderRow>
        <asp:TableRow>
            <asp:TableCell>
                Name<font color="red">*</font>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtName" runat="server" Width="200"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
        <asp:TableCell>
        </asp:TableCell>
            <asp:TableCell><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                     ErrorMessage="Required" Display="Dynamic" ControlToValidate="txtName"
                     ForeColor="Red">
                </asp:RequiredFieldValidator></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
            <asp:TableCell ColumnSpan="2">
                <hr />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
            <asp:TableCell Width="50">
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" BackColor="#999966"  />
            </asp:TableCell>
            <asp:TableCell>
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" BackColor="#999966"  CausesValidation="False" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

    <asp:Label ID="Label1" runat="server" Text="<font color=red>*</font>Required Field" Font-Size="Small"></asp:Label>
    <div style="background-color:Aqua; width:500px; border-color:Red">
        <asp:Label ID="Label2" runat="server" Font-Size="X-Large"></asp:Label>
    </div>
    <br />
    <br />
    <asp:Table ID="tblLanguages" runat="server" BorderWidth="2">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell ColumnSpan="2">
                    Languages
                </asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell Width="20">
                    <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" BackColor="#999966" />
                </asp:TableCell>
                <asp:TableCell>
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" BackColor="#999966" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell BackColor="#FFE4B5">
                    <asp:CheckBox ID="checkbox1" runat="server" />
                </asp:TableCell>
                <asp:TableCell BackColor="#FFE4B5">
                    Name
                </asp:TableCell>
            </asp:TableRow>   
        </asp:Table> 
</form>

添加语言
名字*



语言文字 名称

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{

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



    private void BindTable()
    {
        List<TableRow> testlist = new List<TableRow>();
        foreach (var item in testlist)
        {
            TableRow NewRow1 = new TableRow();
            TableCell NewCell1 = new TableCell();
            CheckBox newCheckBox = new CheckBox();

            NewCell1.Controls.Add(newCheckBox);
            NewRow1.Cells.Add(NewCell1);

            TableCell NewCell2 = new TableCell();

            Label newLable1 = new Label();
            newLable1.Text = txtName.Text;

            NewCell1.Controls.Add(newLable1);
            NewRow1.Cells.Add(NewCell1);
            tblLanguages.Rows.Add(NewRow1);
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        tblAdd.Visible = true;
        btnAdd.Visible = false;
        btnDelete.Visible = false;
        Label1.Visible = true;
        Label2.Visible = false;

        BindTable();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
         tblAdd.Visible = false;
         btnAdd.Visible = true;
         btnDelete.Visible = true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            btnAdd.Visible = true;
            btnDelete.Visible = true;
            Label2.Visible = true;
            tblAdd.Visible = false;
            Label2.Text = "Successfully Added";
            add();

            BindTable();
        }

        txtName.Text = "";
    }

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

        SqlCommand command = new SqlCommand("hrm_AddLanguages2", 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();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统配置;
使用系统数据;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
tblAdd.Visible=false;
标签1.可见=假;
}
私有void BindTable()
{
List testlist=新列表();
foreach(testlist中的var项)
{
TableRow NewRow1=新建TableRow();
TableCell NewCell1=新的TableCell();
CheckBox newCheckBox=newCheckBox();
NewCell1.Controls.Add(newCheckBox);
NewRow1.Cells.Add(NewCell1);
TableCell NewCell2=新的TableCell();
Label newLable1=新标签();
newLable1.Text=txtName.Text;
NewCell1.Controls.Add(newLable1);
NewRow1.Cells.Add(NewCell1);
t语言.Rows.Add(NewRow1);
}
}
受保护的无效btnAdd_单击(对象发送者,事件参数e)
{
tblAdd.Visible=true;
btnAdd.Visible=false;
btnDelete.Visible=false;
标签1.可见=真;
标签2.可见=假;
BindTable();
}
受保护的无效BTN删除\单击(对象发送者,事件参数e)
{
}
受保护的无效btnCancel\u单击(对象发送者,事件参数e)
{
tblAdd.Visible=false;
btnAdd.Visible=true;
btnDelete.Visible=true;
}
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{
如果(第页有效)
{
btnAdd.Visible=true;
btnDelete.Visible=true;
Label2.Visible=true;
tblAdd.Visible=false;
Label2.Text=“已成功添加”;
添加();
BindTable();
}
txtName.Text=“”;
}
公共int add()
{
string strcon=ConfigurationManager.ConnectionStrings[“Dbconnection”].ConnectionString;
SqlConnection SqlConnection=新的SqlConnection(strcon);
SqlCommand=newsqlcommand(“hrm_AddLanguages2”,sqlConnection);
command.CommandType=CommandType.storedProcess;
command.Parameters.Add(“@Name”,SqlDbType.VarChar).Value=txtName.Text;
Add(“@CreatedOn”,SqlDbType.DateTime).Value=DateTime.Now;
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命令;
}
}

请帮助我。

这是因为在单击“添加”按钮后,需要将数据添加到表中。我对你的代码做了如下的轻微修改

protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                btnAdd.Visible = true;
                btnDelete.Visible = true;
                Label2.Visible = true;
                tblAdd.Visible = false;
                Label2.Text = "Successfully Added";
                add();

                BindTable();
            }

            txtName.Text = "";
        }
protected void btnAdd_Click(object sender, EventArgs e)
        {
            tblAdd.Visible = true;
            btnAdd.Visible = false;
            btnDelete.Visible = false;
            Label1.Visible = true;
            Label2.Visible = false;

            BindTable();
        } 
将行添加到表中的新方法(从btnSave_Click事件中删除部分连接线)此新方法

private void BindTable()
        {
            foreach (var item in testList)
            {
                TableRow NewRow1 = new TableRow();
                TableCell NewCell1 = new TableCell();
                CheckBox newCheckBox = new CheckBox();

                NewCell1.Controls.Add(newCheckBox);
                NewRow1.Cells.Add(NewCell1);

                TableCell NewCell2 = new TableCell();

                Label newLable1 = new Label();
                newLable1.Text = item.Name;

                NewCell1.Controls.Add(newLable1);
                NewRow1.Cells.Add(NewCell1);
                tblLanguages.Rows.Add(NewRow1);
            }
        }
此外,您还需要在btnSave_单击结束时调用此方法,如下所示

protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                btnAdd.Visible = true;
                btnDelete.Visible = true;
                Label2.Visible = true;
                tblAdd.Visible = false;
                Label2.Text = "Successfully Added";
                add();

                BindTable();
            }

            txtName.Text = "";
        }
protected void btnAdd_Click(object sender, EventArgs e)
        {
            tblAdd.Visible = true;
            btnAdd.Visible = false;
            btnDelete.Visible = false;
            Label1.Visible = true;
            Label2.Visible = false;

            BindTable();
        } 
最后BindTable()方法如下所示

protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                btnAdd.Visible = true;
                btnDelete.Visible = true;
                Label2.Visible = true;
                tblAdd.Visible = false;
                Label2.Text = "Successfully Added";
                add();

                BindTable();
            }

            txtName.Text = "";
        }
protected void btnAdd_Click(object sender, EventArgs e)
        {
            tblAdd.Visible = true;
            btnAdd.Visible = false;
            btnDelete.Visible = false;
            Label1.Visible = true;
            Label2.Visible = false;

            BindTable();
        } 
这就是你需要做的希望这能帮到你

干杯
愉快的编码…!!!

这是因为您的表在回发后失去了它的状态。您可以阅读有关在中维护对象状态的内容。基本上,您只需执行以下操作:

protected List<TableRow> _rows
{
  get
  {
    List<TableRow> list = (List<TableRow>) ViewState["myRows"];
    if (list != null)
       return list;
    else
       return new List<TableRow>;
  }
  set
  {
    ViewState["myRows"] = value;
  }
}

private override void DataBind()
{
    tblLanguages.Rows = _rows;

}
受保护列表\u行
{
收到
{
列表=(列表)视图状态[“myRows”];
如果(列表!=null)
退货清单;
其他的
返回新列表;
}
设置
{
ViewState[“myRows”]=值;
}
}
私有覆盖无效数据绑定()
{
t语言行=_行;
}

您只需在btnSave\u Click方法的末尾添加
\u rows=tblLanguages.rows
。Ofc您不需要使用ViewState,您可以在DataBind函数中查询数据库,以提取保存的行,然后将它们绑定到表中。View state只帮助您在回发之间维护对象状态,而无需查询您的数据每次都使用数据库。您应该知道,在第一次加载页面时,您需要添加一些查询数据库的代码,以填写语言表的初始状态。

在您的代码中,
btnAdd\u Click
的任务只是使某些控件可见或不可见。一旦单击“添加”按钮,数据就会丢失。这是您的问题吗?是的。。。当我单击“保存”按钮时,保存的数据显示在第二个表中,但当我单击“下一步添加”按钮时,保存的数据将消失。请使用
GridView
而不是
table
。您是否两次问了相同的问题??什么是testlist h