我有 1 个转发器并从Assign_Subjects表中绑定 subjectid 和 subjectname ...并放置1 个文本框模板以插入在每个主题@btnInsert_Click事件中获得的标记,我已经编写了如下插入代码,它工作正常...结果表在我想在主题中插入标记...
我想更新在转发器控件中插入的标记(在文本框 - MarksObtained 列中)...我使用按钮作为 btnUpdate 来编辑文本框...。
.aspx页面
<div>
<table border="0" width="600px" cellpadding="2" cellspacing="1" style="border: 1px solid maroon;">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr bgcolor="maroon">
<th>
Subject_Id
</th>
<th>
Subject_Name
</th>
<th>
Fill_Marks
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="100">
<asp:TextBox ID="txtSubjectId" runat="Server" Text='<%#Eval("Subject_Id")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtSubjectName" runat="Server" Text='<%#Eval("Subject_Name")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtMarks" runat="server" ></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
**<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Insert" />**
</div>
后面的代码 - c#
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)
现在的问题是我如何通过中继器更新???用更适用的代码解释...谢谢