1

I have an user control that contains an update panel with a repeater. The repeater has a button used to delete records.

The problem is when I press the delete button, and a record from the repeater gets deleted, a postback on the whole page is triggered. From my understanding, only the section inside the update panel should refresh.

I should also mention that on my master page I have a script manager with the property "EnablePartialRendering" set to true.

Can someone help me with this problem? Thanks in advance.

ASP file:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ActivityFiles.ascx.cs" Inherits="Training.User_Controls.ActivityFiles" %>

<asp:Repeater ID="FilesRepeater" runat="server">
    <HeaderTemplate>
        <table class="table table-bordered">
            <tr>
                <td><b>Name</b></td>
                <td><b>Description</b></td>
                <td><b>Actions</b></td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <tr>
                    <td><%# Eval("Name")%></td>
                    <td><%# Eval("Description")%></td>
                    <td>
                        <asp:LinkButton ID="DownloadFile" runat="server" OnClick="DownloadFile_Click" Font-Size="Large"
                            file-path='<%# Eval("Url")%>'>
                            <span class="glyphicon glyphicon-floppy-disk text-info" aria-hidden="true"></span>
                        </asp:LinkButton>

                        <asp:LinkButton ID="DeleteFile" runat="server" OnClick="DeleteFile_Click" Font-Size="Large"
                            file-id='<%# Eval("Id") %>'
                            file-path='<%# Eval("Url") %>'>
                            <span class="glyphicon glyphicon-trash text-danger" aria-hidden="true"></span>
                        </asp:LinkButton>
                    </td>
                </tr>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

.cs File:

        protected void Page_Load(object sender, EventArgs e)
        {
            activityId = (int)Context.Items["activityId"];
            if ( ! IsPostBack && Visible)
            {
                if (activityId != 0)
                {
                    LoadFiles();
                }
            }
        }

        private void LoadFiles()
        {
            DataTable files = DatabaseHelper.SelectAsDatatable(FilesQueries.GET_ACTIVITY_FILES, new Dictionary<string, object> {
                { "activityId", activityId }
            });

            FilesRepeater.DataSource = files;
            FilesRepeater.DataBind();

            if (files.Rows.Count == 0)
            {
                FilesRepeater.Visible = false;
                NoRecords.Visible = true;
            }
        }

        protected void DeleteFile_Click(object sender, EventArgs e)
        {
            // TODO: modify this function to work only with the fild id
            LinkButton deleteButton = (LinkButton)sender;

            string fileId = deleteButton.Attributes["file-id"];
            string filePath = deleteButton.Attributes["file-path"];

            DatabaseHelper.ExecuteNonQuery(FilesQueries.DELETE_FILE, new Dictionary<string, Object>() {
                { "@fileId",fileId },
            });

            //delete file from disk
            string path = Server.MapPath(filePath);
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                file.Delete();
            }

            LoadFiles();
        }
Fishlex
  • 46
  • 7
  • Try putting the UpdatePanel outside the Repeater instead of in the ContentTemplate. – VDWWD Mar 18 '21 at 19:13
  • Thanks for your suggestion. I just tried that but sadly it did not have any visible effect. It may have some connections with the fact that the repeater is in a user control? – Fishlex Mar 18 '21 at 19:55

1 Answers1

0

Found the solution on another stackoverflow post (See Eugene S answer)

You have to manually add you control to Script Manager. You should do that inside File Repeater's Item created event. Good luck.

Fishlex
  • 46
  • 7