[英]How To Use Update Function In Repeater Control Dynamically?
我有1個轉發器,並綁定了Assign_Subjects表中的subjectid和subjectname ...,並放置了1個文本框模板以插入在每個subject @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)
現在的問題是我如何通過中繼器進行更新??? 解釋與更多的應用程序的代碼...謝謝
您的中繼器內的按鈕應該看起來像這樣.....
<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>
然后使用Repeater的ItemCommand事件捕獲由Repeater控件生成的事件
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..
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.