Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a repeater for my site in ASP.NET and tried adding a button which should delete a row from the repeater, but clicking the button doesn't do anything besides refreshing the page.

Repeater markup
ASP
<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div class="detail">
Namn: <asp:Label ID="Label1" runat="server" Text='<%#Eval("Namn") %>'>'></asp:Label><br />
Address <asp:Label ID="Label3" runat="server" Text='<%#Eval("Adress") %>'></asp:Label><br />
Telefonnummer <asp:Label ID="Label4" runat="server" Text='<%#Eval("Telefonnummer") %>'></asp:Label><br />
Mailaddres <asp:Label ID="Label5" runat="server" Text='<%#Eval("Epost-address") %>'></asp:Label><br />
Meddelande: <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>


Code behind:
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Admin : System.Web.UI.Page
{
    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);
        }
    }
    private 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");
        string str = "DELETE FROM [Table] WHERE [Id] = @Id";
        SqlCommand sqlcm = new SqlCommand(str, 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();
    }
}


Any ideas?

What I have tried:

I have tried different variations of the deleting code but none have worked.
Posted
Updated 21-Feb-16 22:37pm

You missing any code to handle the click, so it does a postback, that no one listens to...
Check on sample here:
LinkButton.OnClick Method (EventArgs) (System.Web.UI.WebControls)[^]
 
Share this answer
 
You need to attach your event, .net doesn't intrinsically know which function to call when the link button is clicked.

<asp:Repeater OnItemCommand="rpt_ItemCommand" ....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900