我有这个转发器
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="detail">
<asp:Label ID="LabelID" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "Id") %>'>'></asp:Label><br />
<b>Namn:</b> <asp:Label ID="Label1" runat="server" Text='<%#Eval("Namn") %>'>'></asp:Label> <br />
<b>Address</b> <asp:Label ID="Label3" runat="server" Text='<%#Eval("Adress") %>'></asp:Label><br />
<b>Telefonnummer</b> <asp:Label ID="Label4" runat="server" Text='<%#Eval("Telefonnummer") %>'></asp:Label><br />
<b>Mailaddres</b> <asp:Label ID="Label5" runat="server" Text='<%#Eval("Epost-address") %>'></asp:Label><br />
<b>Meddelande:</b> <asp:Label ID="Label6" runat="server" Text='<%#Eval("Kommentar") %>'></asp:Label><br />
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="Delete" onclientclick="return confirm('Are you sure you want to delete?')">Delete</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
这就是它背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete") {
int id = Convert.ToInt32(e.CommandArgument);
deleteKlag(id);
}
}
void deleteKlag(int id)
{
SqlConnection sqlcn = new SqlConnection(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework");
SqlCommand sqlcm = new SqlCommand("DELETE FROM [Table] WHERE [Id] = @Id", sqlcn);
sqlcm.Parameters.AddWithValue("@Id", id);
if (sqlcn.State == ConnectionState.Closed)
{
sqlcn.Open();
}
sqlcm.ExecuteNonQuery();
sqlcn.Close();
BindRepeater();
}
protected void BindRepeater()
{
SqlConnection con = new SqlConnection(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework");
string command = "Select * from [Table]";
SqlCommand cmd = new SqlCommand(command, con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();
}
它显示数据库中的所有项目都很好,但是当我按下“删除”按钮时没有任何反应。我尝试了很多不同的变化,但它们最终都无所作为。有什么想法吗?
答案 0 :(得分:0)
OnItemCommand="rpt_ItemCommand"