简体   繁体   中英

Can't delete item from repeater

I have this repeater

<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>&nbsp;<asp:Label ID="Label1" runat="server" Text='<%#Eval("Namn") %>'>'></asp:Label>&nbsp;<br />
                    <b>Address</b>&nbsp;<asp:Label ID="Label3" runat="server" Text='<%#Eval("Adress") %>'></asp:Label><br />
                    <b>Telefonnummer</b>&nbsp;<asp:Label ID="Label4" runat="server" Text='<%#Eval("Telefonnummer") %>'></asp:Label><br />
                    <b>Mailaddres</b>&nbsp;<asp:Label ID="Label5" runat="server" Text='<%#Eval("Epost-address") %>'></asp:Label><br />
                    <b>Meddelande:</b>&nbsp;<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>

And this is the code behind it:

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();
}

It shows all the items in the database fine, but when I press the "Delete" button nothing happens. I have tried many different variations but they all end up doing nothing. Any ideas?

Ahhhhh I forgot to put

OnItemCommand="rpt_ItemCommand"

on my Repeater tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM