C# 如何在中继器控制中动态使用更新功能?
我有一个转发器,从Assign_Subjects表中绑定subjectid和subjectname…并放置一个文本框模板来插入在每个Subjects@btnisert_Click事件中获得的标记,我已经按照下面的方式编写了插入代码,效果很好…我想在结果表中插入标记和主题 我想更新标记,即插入到文本框中的标记-中继器控件中的标记包含列…我使用按钮作为编辑文本框的btnUpdate .aspx页 插入到结果表中C# 如何在中继器控制中动态使用更新功能?,c#,asp.net,repeater,edit,insert-update,C#,Asp.net,Repeater,Edit,Insert Update,我有一个转发器,从Assign_Subjects表中绑定subjectid和subjectname…并放置一个文本框模板来插入在每个Subjects@btnisert_Click事件中获得的标记,我已经按照下面的方式编写了插入代码,效果很好…我想在结果表中插入标记和主题 我想更新标记,即插入到文本框中的标记-中继器控件中的标记包含列…我使用按钮作为编辑文本框的btnUpdate .aspx页 插入到结果表中 现在的问题是如何通过中继器更新???用代码解释更精确。。。谢谢中继器内的按钮应该是这样的
现在的问题是如何通过中继器更新???用代码解释更精确。。。谢谢中继器内的按钮应该是这样的
<td>
<asp:ImageButton ID="btnSave" runat="server"
ImageUrl="~/images/save.png" ToolTip="click to save record"
CommandName="SaveData"
OnClientClick="return confirm('Conform message before saving')" />
</td>
首先,您应该使用参数来构建SqlCommand,否则您会受到SQL注入攻击。第二,你得到的确切错误是什么?我对你试图实现的目标感到困惑。你有一个中继器,我假设你用数据填充。然后,当您单击“插入”时,它将循环遍历中继器中的每个项目,并将其插入到您的数据库中?我想你可能是想把文本框放在中继器下面,向列表中添加一个新项目?kevin main-我更新了我的问题…你可以看到它…在插入后,你正在重新绑定中继器,这意味着中继器1中的数据。项目集合发生更改,因此出现错误。删除SaveData方法中的绑定。只有在PageLoad中绑定转发器(如果不是回发的话)才能解决此问题
protected void btnInsert_Click(object sender, EventArgs e)
{
foreach (RepeaterItem repeaterItem in Repeater1.Items)
{
string subjectId = (repeaterItem.FindControl("txtSubjectId") as TextBox).Text.Trim();
string subjectName = (repeaterItem.FindControl("txtSubjectName") as TextBox).Text.Trim();
string marks = (repeaterItem.FindControl("txtMarks") as TextBox).Text.Trim();
this.SaveData(subjectId, subjectName, marks);
}
}
private void SaveData(string subjectId, string subjectName, string marks)
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into result(id,name,marks) values('"+subjectId+"','"+subjectName+"','"+marks+"')", cn);
Repeater1.DataSource = cmd.ExecuteReader();
//Display a popup which indicates that the record was successfully inserted
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Successfuly Inserted. !!');", true);
cn.Close();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
id varchar(20)
name varchar(20)
marks varchar(20)
<td>
<asp:ImageButton ID="btnSave" runat="server"
ImageUrl="~/images/save.png" ToolTip="click to save record"
CommandName="SaveData"
OnClientClick="return confirm('Conform message before saving')" />
</td>
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// note that this is the command we assign to save button.
// we use it here to capture which button was clicked.
if (e.CommandName == "SaveData")
{
TextBox txtFirstName = e.Item.FindControl("txtFirstName") as TextBox;
string fname= txtFirstName.Text;
// do somethig with your date here..
}
}