<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function GetDynamicTextBox(value)
{
return '<input type="CheckBox"/> '+'<input name="DynamicTextBox1"
type="text" value = "' + value + '" /> '+'<input type="button"
value="Remove" onclick=" RemoveTextBox(this)" />'
}
function AddTextBox()
{
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}
function RemoveTextBox(div)
{
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}
function RecreateDynamicTextboxes()
{
var values = eval('<%=Values%>');
if (values != null)
{
var html = "";
for (var i = 0; i < values.length; i++)
{
html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
</script>
</head>
<body>
<form id="Form1" runat="server">
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
<br />
<input id="btnAdd" type="button" value="Add Text" onclick="AddTextBox()" />
<br /><br />
<asp:Button ID="btnPost" runat="server" Text="submit" OnClick="Post" />
</form>
</body>
</html>
代码背后:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization;
namespace Row_By_Button
{
public partial class WebForm1 : System.Web.UI.Page
{
protected string Values;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Post(object sender, EventArgs e)
{
string[] textboxValues1 = Request.Form.GetValues("DynamicTextBox1");
JavaScriptSerializer serializer = new JavaScriptSerializer();
this.Values = serializer.Serialize(textboxValues1);
}
}
}
答案 0 :(得分:0)
要添加控件,请执行以下操作:调整此选项以根据您的请求添加更多控件
function addTextBox()
{
var inpt = document.getElementById('input_template');//parent Div name
element.setAttribute("id", "TextBox1");
element.setAttribute("type", "text");
element.setAttribute("value", "");
element.setAttribute("name", "Test Name");
inpt.appendChild(element);
}
检索并插入db
string s = Request.Form["TextBox1"].ToString(); //C#