locked
How to Delete row with link button in repeater RRS feed

  • Question

  • User1717218719 posted

    I have the following code which doesnt work at the moment. I am looking to delete a row based on linkbutton clicked. I am unsure as to how to go about this. My guess its similar to the update code just the opposite. any help would be great

    If CType(e.CommandSource, LinkButton).CommandName = "DeleteRow" Then
                    Dim intIdx As Integer = CType(e.CommandSource, LinkButton).CommandArgument
                    Dim itmRpt As RepeaterItem = Me.rptTaxCom.Items(intIdx)
    
                    '-- Validation - validate fields (integers/decimals)
                    Dim txtAir As TextBox = CType(itmRpt.FindControl("TextBox2"), TextBox)
                    Dim txtAGT As TextBox = CType(itmRpt.FindControl("TextBox3"), TextBox)
                    Dim txtDIF As TextBox = CType(itmRpt.FindControl("TextBox4"), TextBox)
    
                    CType(itmRpt.FindControl("TextBox1"), TextBox).Text = 'REMOVE ROW CODE?
                    CType(itmRpt.FindControl("TextBox2"), TextBox).Text = 'REMOVE ROW CODE?
                    CType(itmRpt.FindControl("TextBox3"), TextBox).Text = 'REMOVE ROW CODE?
                    CType(itmRpt.FindControl("TextBox4"), TextBox).Text = 'REMOVE ROW CODE?
    
    
                End If



    <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/> 
    



    Friday, June 14, 2019 8:41 AM

Answers

  • User288213138 posted

    Hi E.RU,

    In order to delete row from ASP.Net repeater without using database,I use temporary DataTable stored in ViewState. Then use the Delete method to delete the row of the current index according to the index of the row.

     The code:

    Aspx:
    <div>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <HeaderTemplate>
                        <table border="1">
                            <tr>
                                <th scope="col" style="width: 80px">CustomerId
                                </th>
                                <th scope="col" style="width: 120px">Name
                                </th>
                                <th scope="col" style="width: 100px">Country
                                </th>
                                <th></th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerId") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Country") %>' />
                            </td>                     
                            <td>
                                <asp:LinkButton ID="lnkUpdate" Text="Delete" runat="server" OnClick="lnkUpdate_Click" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
    
    Aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "name1", "country1");
                    dt.Rows.Add(2, "name2", "country2");
                    dt.Rows.Add(3, "name3", "country3");
                    ViewState["dt"] = dt;
                    Bind();
                }
            }
            public void Bind()
            {
                Repeater1.DataSource = ViewState["dt"] as DataTable;
                Repeater1.DataBind();
            }
    
            protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                int index = Convert.ToInt32(e.CommandArgument);
                DataTable dt = ViewState["dt"] as DataTable;
                dt.Rows[index].Delete();
                ViewState["dt"] = dt;
                Bind();
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 18, 2019 8:49 AM

All replies

  • User288213138 posted

    Hi E.RU,

    According to your needs, I wrote a demo you can use as a reference, but because I don't have your aspx code, I can't completely reproduce your problem.

    The code:

    Aspx:
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <HeaderTemplate>
                        <table border="1">
                            <tr>
                                <th scope="col" style="width: 80px">CustomerId
                                </th>
                                <th scope="col" style="width: 120px">Name
                                </th>
                                <th scope="col" style="width: 100px">Country
                                </th>
                                <th></th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerId") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Country") %>' />
                            </td>                     
                            <td>
                                <asp:LinkButton ID="lnkUpdate" Text="Delete" runat="server" OnClick="lnkUpdate_Click" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
    
    Aspx.cs:
    Class SurroundingClass
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                Bind()
            End If
        End Sub
    
        Private str As String = ConfigurationManager.ConnectionStrings("constr527").ConnectionString
    
        Public Sub Bind()
            Dim sql As String = "Select * from Customer"
    
            Using con As SqlConnection = New SqlConnection(str)
    
                Using adapter As SqlDataAdapter = New SqlDataAdapter(sql, con)
                    Dim dt As DataTable = New DataTable()
                    adapter.Fill(dt)
                    Repeater1.DataSource = dt
                    Repeater1.DataBind()
                End Using
            End Using
        End Sub
    
        Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
            If e.CommandName = "DeleteRow" Then
                Dim CustomerId As Integer = Convert.ToInt32(e.CommandArgument)
                Dim con As SqlConnection = New SqlConnection(str)
                Dim cmdText As String = "DELETE FROM Customer WHERE CustomerId=@CustomerId"
                Dim cmd As SqlCommand = New SqlCommand(cmdText, con)
                cmd.Parameters.AddWithValue("@CustomerId", CustomerId)
    
                If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
    
                cmd.ExecuteNonQuery()
                con.Close()
                Bind()
            End If
        End Sub
    End Class

    The result:

    Best regards,

    Sam

    Monday, June 17, 2019 5:42 AM
  • User1717218719 posted

    Thank you Samwu so much for your demo its great ! unfortunatly I am not using an sql table / database. instead I manuallly add to the table and then want to delet from this same table. do you know how I would code for this event ?

    Mant thanks

    Monday, June 17, 2019 7:58 AM
  • User288213138 posted

    Hi E.RU,

    In order to delete row from ASP.Net repeater without using database,I use temporary DataTable stored in ViewState. Then use the Delete method to delete the row of the current index according to the index of the row.

     The code:

    Aspx:
    <div>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <HeaderTemplate>
                        <table border="1">
                            <tr>
                                <th scope="col" style="width: 80px">CustomerId
                                </th>
                                <th scope="col" style="width: 120px">Name
                                </th>
                                <th scope="col" style="width: 100px">Country
                                </th>
                                <th></th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerId") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>' />                            
                            </td>
                            <td>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Country") %>' />
                            </td>                     
                            <td>
                                <asp:LinkButton ID="lnkUpdate" Text="Delete" runat="server" OnClick="lnkUpdate_Click" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
    
    Aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "name1", "country1");
                    dt.Rows.Add(2, "name2", "country2");
                    dt.Rows.Add(3, "name3", "country3");
                    ViewState["dt"] = dt;
                    Bind();
                }
            }
            public void Bind()
            {
                Repeater1.DataSource = ViewState["dt"] as DataTable;
                Repeater1.DataBind();
            }
    
            protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                int index = Convert.ToInt32(e.CommandArgument);
                DataTable dt = ViewState["dt"] as DataTable;
                dt.Rows[index].Delete();
                ViewState["dt"] = dt;
                Bind();
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 18, 2019 8:49 AM