[英]Insert data from a Repeater's textbox into a database
I am using a Repeater that has a label and textbox. 我正在使用具有标签和文本框的中继器。 It generates the correct labels based on columns in a table (First Name, Last Name, Address, etc).
它根据表中的列(名字,姓氏,地址等)生成正确的标签。 The user is meant to type in their information into the corresponding textbox.
用户应将其信息输入相应的文本框中。
After this, I need a way to insert their data from the textbox into my database. 在此之后,我需要一种将文本框中的数据插入数据库的方法。 However what I am unsure of is exactly how to insert each item into the correct column of the database.
但是我不确定到底是如何将每个项目插入数据库的正确列中。 I am using Stored Procedures, so it would be something like:
我正在使用存储过程,因此它将类似于:
sqlCmd.Parameters.Add("@FirstName", SqlDbType.Int).Value = firstName.Text;
Of course I cannot identify firstName.Text when the only textbox I have is a txtData repeated. 当然,当我拥有的唯一文本框是重复的txtData时,我无法识别firstName.Text。
foreach (RepeaterItem rpItem in RepeaterForm.Items)
{
Label lblData = rpItem.FindControl("lblData") as Label;
TextBox txtData = rpItem.FindControl("txtData") as TextBox;
if (txtData != null)
{
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "spInsFormFields";
sqlCmd.Parameters.Clear();
//Code here
sqlCmd.ExecuteNonQuery();
}
}
A sample repeater form could look like this: 样本中继器表单可能如下所示:
First Name: John
名:约翰
Last Name: Smith
姓氏:史密斯
Address: 123 Cherry Lane
地址:樱桃巷123号
And a sample of my database is like this: 我的数据库示例如下:
Registrant table
注册人表
FirstName, LastName, Address columns
名,姓,地址列
Submit button should enter ONE row based on an EventId
and FormId
(which I collected earlier in my code) 提交按钮基于一个应该输入一行
EventId
和FormId
(我在我的代码前面收集)
EDIT: 编辑:
Here is my Repeater code (as requested): 这是我的Repeater代码(根据要求):
<asp:Repeater ID="RepeaterForm" runat="server" OnItemCommand="RepeaterForm_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td><asp:Label ID="lblData" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label></td>
<td><asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br></br></td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate><asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="BtnSubmit_Click" /></FooterTemplate>
</asp:Repeater>
Prepare your repeater like this: 像这样准备中继器:
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<input type="text" name='<%#Eval("ColumnName") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
</div>
</form>
And by using this C# code you can get all the values dynamicly : 通过使用此C#代码,您可以动态获取所有值:
public partial class WebForm1 : System.Web.UI.Page
{
List<Column> Columns = new List<Column>();
protected void Page_Init(object sender, EventArgs e)
{
Columns.Add(new Column { ColumnName = "ID" });
Columns.Add(new Column { ColumnName = "Name" });
Columns.Add(new Column { ColumnName = "Surname" });
}
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = Columns;
Repeater1.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
Dictionary<string, string> Values = new Dictionary<string, string>();
foreach (var item in Columns)
{
Values.Add(item.ColumnName, Request.Form[item.ColumnName]);
}
}
}
class Column
{
public string ColumnName { get; set; }
}
Assume that txtFirstname
, txtLastname
, txtAddress
are the names for respective controls in the repeater, then You can access them by using following code: 假设
txtFirstname
, txtLastname
, txtAddress
是转发器中各个控件的名称,则可以使用以下代码访问它们:
foreach (RepeaterItem rpItem in RepeaterForm.Items)
{
TextBox tFirstname = rpItem.FindControl("txtFirstname") as TextBox;
TextBox tLastname = rpItem.FindControl("txtLastname") as TextBox;
TextBox tAddress = rpItem.FindControl("txtAddress") as TextBox;
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "spInsFormFields";
sqlCmd.Parameters.Clear();
if (tFirstname != null)
sqlCmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = tFirstname.Text;
if (tLastname != null)
sqlCmd.Parameters.Add("@Lastname", SqlDbType.VarChar).Value = tLastname.Text;
if (tAddress != null)
sqlCmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = tAddress.Text;
sqlCmd.ExecuteNonQuery();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.