[英]SQL string loop for generated fields on ASP.Net webform
I have the following ASP.Net webform that has a button which can be clicked by the user to generate a bunch of fields (using Javascript) on the form. 我有以下具有按钮的ASP.Net网络表单,用户可以单击该按钮在表单上生成一堆字段(使用Javascript)。 I also have a bunch of static fields on the form and the values entered on the static fields are transferred to a SQL table.
我在表单上也有一堆静态字段,并且在静态字段上输入的值被传输到SQL表。 I am having great troubles transferring values that are being entered on the generated fields with a button click.
我很难通过按钮单击来传输在生成的字段上输入的值。 As of now, I'm naming the generated textboxes as txtDimension 2 & so on.
到目前为止,我将生成的文本框命名为txtDimension 2,依此类推。 I need help to create a SQL string loop that would help me transfer values from both the generated and static fields on the webform.
我需要帮助来创建一个SQL字符串循环,该循环将帮助我从Web窗体上的已生成字段和静态字段中传输值。
This is my ASP.Net button code: 这是我的ASP.Net按钮代码:
<script src="check.js" type="text/javascript"></script>
<div id="divDimensions" class="auto-style9">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Add More Fields" OnClientClick="return addInput('divDimensions');" CssClass="auto-style10" Height="68px" Width="236px" />
</div>
<p class="auto-style9">
</p>
<p class="auto-style9">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Width="191px" Height="82px" />
</p>
Javascript code for the ASP.Net button that the user clicks to generate additional fields on the webform: 用户单击以在Web窗体上生成其他字段的ASP.Net按钮的Java代码:
var counter = 1;
var limit = 1000;
function addInput(divName) {
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var table = document.getElementById("tableChecksheet");
var row = table.insertRow();
var cell1 = row.insertCell(0);
cell1.className = "auto-style3"
var cell2 = row.insertCell(1);
cell1.innerHTML = "<h2>Dimension Type " + (counter + 1);
cell2.innerHTML = "<br><input id='txtdimensiontype " + (counter + 1) + "' type='text' name='myInputs[]' style='width: 500px;'>";
var row = table.insertRow();
var cell1 = row.insertCell(0);
cell1.className = "auto-style3"
var cell2 = row.insertCell(1);
cell1.innerHTML = "<h2>Dimension " + (counter + 1);
cell2.innerHTML = "<br><input id='txtdimension " + (counter + 1) + "' type='text' name='myInputs[]' style='width: 500px;'>";
counter++;
}
}
C# code behind webform that transfers values entered in the static fields from the webform as of now, I want a SQL string loop for the generated fields: 截至目前,Webform背后的C#代码可从Webform传输在静态字段中输入的值,我希望生成的字段使用SQL字符串循环:
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;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var connectionString = @strConn;
var query = @"INSERT INTO Checksheets (DimensionType, Dimension) VALUES (@sixthword,@seventhword);";
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = new SqlCommand(query, connection))
{
command.Parameters.Add(new SqlParameter("@sixthword", txtdimensiontype.Text));
command.Parameters.Add(new SqlParameter("@seventhword", txtdimension.Text));
var Reader = command.ExecuteReader();
}
}
{
Response.Redirect(Request.RawUrl);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
-- for table type to pass to SP
CREATE TYPE [dbo].[NameValuePairTable] AS TABLE( DimensionType [NVARCHAR](MAX) NULL, Dimension [NVARCHAR](MAX) NULL ) GO
-- call SP from C# code and pass it the table type object
CREATE PROCEDURE [dbo].[SaveResultsFromForm]
@NameValuePairAnswers as dbo.NameValuePairTable READONLY
AS
BEGIN
-- this will insert ALL answers from the web form you passed from the c# object
INSERT INTO Checksheets (DimensionType, Dimension)
Select DimensionType, Dimension
FROM @NameValuePairAnswers
END
c# C#
// now build out the namevalue pair paramater to pass to SP call
SqlParameter TableData = new SqlParameter();
TableData.ParameterName = "@NameValuePairAnswers";
TableData.TypeName = "dbo.NameValuePairTable";
TableData.SqlDbType = SqlDbType.Structured;
// this will be the C# object you populated from your form as a C# datatype DataTable
TableData.Value = FormResultsDataTable;
updated to add more c# code 更新以添加更多的C#代码
string SQL = "EXEC dbo.SaveResultsFromForm @NameValuePairAnswers = @NameValuePairAnswers";
SqlCommand InsertDataCommand = new SqlCommand(sSQL);
// now build out the namevalue pair paramater
SqlParameter TableData = new SqlParameter();
TableData.ParameterName = "@NameValuePairAnswers";
TableData.TypeName = "dbo.NameValuePairTable";
TableData.SqlDbType = SqlDbType.Structured;
TableData.Value = FormResultsDataTable;
// now add the tabledata to the list
InsertDataCommand.Parameters.Add(TableData);
// to execute the command to SQL
InsertDataCommand.Execute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.