我正在使用具有标签和文本框的Repeater。它根据表中的列(名字,姓氏,地址等)生成正确的标签。用户打算在相应的文本框中键入他们的信息。
在此之后,我需要一种方法将文本框中的数据插入到我的数据库中。但是,我不确定的是如何将每个项目插入到数据库的正确列中。我正在使用存储过程,所以它会是这样的:
sqlCmd.Parameters.Add("@FirstName", SqlDbType.Int).Value = firstName.Text;
当我拥有的唯一文本框是重复的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();
}
}
示例转发器表单可能如下所示:
名字:约翰
姓氏:史密斯
地址:123 Cherry Lane
我的数据库样本是这样的:
注册人表
FirstName,LastName,Address columns
提交按钮应根据EventId
和FormId
(我之前在我的代码中收集)输入一行
修改
这是我的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>
答案 0 :(得分:3)
准备这样的转发器:
<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>
通过使用此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; }
}
答案 1 :(得分:1)
假设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();
}