0
我有1箇中繼器和綁定subjectid和主旨名稱從Assign_Subjects表...並把1文本框模板中插入每個科目@btnInsert_Click事件,我已經寫了我插入如下代碼獲得商標及它工作正常...和結果表中,我要與主體沿插入標記......如何在直放站控制中動態使用更新功能?
我想更新即插入標記(在文本框 - MarksObtained列)中繼器控制範圍之內.. .i使用按鈕作爲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)
現在的問題是我怎樣才能通過中繼器更新?更appriciated代碼解釋...感謝
首先,您應該使用參數來構建您的SqlCommand,否則您會打開SQL注入攻擊。其次 - 你得到的確切錯誤是什麼? –
我很困惑你想完成什麼。你有一箇中繼器,我假設你用數據填充。然後,當你點擊Insert時,它會遍歷中繼器中的每個項目,並對數據庫執行INSERT操作?我認爲相反,你可能意味着將文本框放在中繼器的下方,以將新項目添加到列表中? –
凱文主 - 我更新了我的問題...你可以看到它... – mack28