您好我有一个包含静态和动态文本框的网络表单。我已经编写了一个代码来创建无限的动态文本框(最多使用31个,但我给了用户灵活性)但是现在我必须在数据库中插入动态创建的文本框的值
假设我在 1 个文本框中填充了一些文本,它会自动创建下一个文本框,即,只要我在第一个文本框中输入 1 个字符,它就会生成新的文本框,最后当我单击提交时,我希望文本框中的所有这些数据都存储在数据库中.
它应该为每个生成的新文本框创建新的字段/列,但如果它在最大限制内,那么它应该在先前创建的列中插入数据。任何解决方案?
这是我的代码: WebForm1.aspx
<script type="text/javascript">
getId = function ()
{
var id = 1;
return function ()
{
id++;
}
}
function CreateTextbox()
{
var box = document.getElementById("divCreateTextbox");
var curr = 'txt' + getId();
var inp = document.createElement('input');
inp.type = 'text';
inp.name = 'textfield';
inp.setAttribute("id", 'curr');
inp.setAttribute("minlength", '1');
box.appendChild(inp);
inp.setAttribute('onkeyup', 'moveOnMin(this)');
inp.setAttribute("textBoxAdded", "0");
inp.focus();
}
function moveOnMin(s)
{
if (s.value.length == parseInt(s.getAttribute("minlength")) && s.getAttribute("textBoxAdded") == "0")
{
CreateTextbox();
s.setAttribute("textBoxAdded", "1");
s.focus();
}
}
<div id="divCreateTextbox"></div>
<script type="text/javascript">
window.onload = function ()
{
CreateTextbox()
}
WebForm1.aspx.cs(将静态文本框文本插入 sql 的代码)
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection connection = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Tbl Return's Tracker]", connection);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataBind();
}
}
}
protected void BtnSubmit_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand("INSERT INTO [Tbl Return's Tracker] ([Company],[Date],[Month],[OrderReference],[Product],[Status],[ReturnLabel],[NumberOfLenses],[Shelf], [ReshipmentNumber],[Reason1],[Reason2],[ActionTaken],[ReturnHandledby])VALUES (@company,@date,@month,@orderreferencenumber,@product,@status,@returnlabel,@numberoflenses,@shelf,@reshipmentordernumber,@Reason1,@Reason2,@action,@name)", connection);
command.Parameters.AddWithValue("@company", DdlCompany.SelectedItem.Text);
command.Parameters.AddWithValue("@month", LblMonth.Text);
command.Parameters.AddWithValue("@date", LblDate.Text);
command.Parameters.AddWithValue("@orderreferencenumber", TxtOrderReferenceNo.Text);
command.Parameters.AddWithValue("@product", DdlProduct.SelectedItem.Text);
command.Parameters.AddWithValue("@status", DdlStatus.SelectedItem.Text);
command.Parameters.AddWithValue("@returnlabel", DdlReturnLabel.SelectedValue);
command.Parameters.AddWithValue("@numberoflenses", TxtNumberOfLenses.Text);
command.Parameters.AddWithValue("@shelf", DdlShelf.SelectedItem.Text);
command.Parameters.AddWithValue("@reshipmentordernumber", TxtReshipmentOrderNo.Text);
command.Parameters.AddWithValue("@Reason1", DdlReason1.SelectedItem.Text);
command.Parameters.AddWithValue("@Reason2", DdlReason2.SelectedItem.Text);
command.Parameters.AddWithValue("@action", DdlAction.SelectedItem.Text);
command.Parameters.AddWithValue("@Name", DdlName.SelectedItem.Text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
GridView1.DataBind();
}