我试图在asp表格中添加表格行但无法解决它。在asp.net中动态添加表格行在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();
}
}
请帮助我。
在你的代码中,'btnAdd_Click'的作用就是让一些控件可见或不可见。 –
点击添加按钮后数据丢失。这是你的问题吗? – shana
是的...当我点击保存按钮,保存的数据显示在第二个表中,但是当我点击添加按钮,我保存的数据消失。 –