今天做项目,涉及到动态添加表格,研究了下可以使用后台动态添加实现,其中代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bb.aspx.cs" Inherits="BAS_bb" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" runat="server">
<tr>
<td>问题</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="40" Width="400"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnOK" runat="server" Text="添加" OnClick="btnOK_Click" Width="50" />
</td>
</tr>
</table>
<div><asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click"/></div>
</div>
</form>
</body>
</html>
后台C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using COTP.DAL;
public partial class BAS_bb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToInt32(ViewState["zp"]) != 0) //判断是否是第一次添加
{
for (int i = 1; i <= Convert.ToInt32(ViewState["zp"].ToString()); i++)
{
AddTextBoxs();
}
}
}
//添加行事件
protected void btnOK_Click(object sender, EventArgs e)
{
AddTextBoxs();
ViewState["zp"] = Convert.ToInt16(ViewState["zp"]) + 1;
TextBox txtzpName = (TextBox)Table1.Rows[Table1.Rows.Count - 1].FindControl("txtzpName" + (Table1.Rows.Count - 1));
}
//动态添加行
private void AddTextBoxs()
{
HtmlTableRow tr = new HtmlTableRow();
//添加选项名称
HtmlTableCell tc1 = new HtmlTableCell();
Label lb = new Label();
lb.Text = "选项" + Table1.Rows.Count;
tc1.Controls.Add(lb);
//添加名称文本框。。
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTextArea txtzpName = new HtmlTextArea();
txtzpName.ID = "txt" + Table1.Rows.Count;
txtzpName.Attributes.Add("style", "width:400px;height:40px;");
txtzpName.Attributes.Add("ruant", "server");
tc2.Controls.Add(txtzpName);
//添加按钮
HtmlTableCell tc4 = new HtmlTableCell();
Button btn = new Button();
btn.ID = "bt" + Table1.Rows.Count;
btn.Text = "删除";
btn.Width = 60;
btn.CommandArgument = Table1.Rows.Count.ToString();
btn.Click += new System.EventHandler(bt_Click);
tc4.Controls.Add(btn);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc4);
Table1.Rows.Add(tr);
int bb = Table1.Rows.Count;
}
//注册的事件
private void bt_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
Table1.Rows[Convert.ToInt16(btn.CommandArgument)].Visible = false;
}
//将添加的数据保存到数据库
protected void Button1_Click(object sender, EventArgs e)
{
string txtvalue=string.Empty;
bool result = false;
for (int i = 1; i <= Convert.ToInt32(ViewState["zp"].ToString()); i++)
{
if (Request.Form["txt" + i] != null)
{
txtvalue+= Request.Form["txt" + i].ToString()+","; // 获取到所有动态添加的文本框数据,以“,”分隔
}
}
string [] arr = txtvalue.Split(','); //将获取的动态文本框的值放在一个字符串数组中
string a = string.Empty;
for (int i = 0; i < arr.Length; i++)//有多少条数据则数据库插入多少条数据
{
if (arr[i].ToString() != "")
{
a = arr[i].ToString();
if (Addtest(a))
{
result = true;
}
else
{ result = false; }
}
}
if (result == true)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('成功');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('失败');", true);
}
}
/// <summary>
/// 增加一条数据
/// </summary>
public bool Addtest(string item)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into item(item)values (@item)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@item", SqlDbType.NVarChar,100),
};
parameters[0].Value = item;
int rows = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text,
strSql.ToString(), parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
}