【问题标题】:Repeater causes postback of the whole page even if it's inside an update panel转发器会导致整个页面的回发,即使它位于更新面板内
【发布时间】:2021-06-15 21:43:54
【问题描述】:

我有一个用户控件,其中包含一个带有中继器的更新面板。中继器有一个用于删除记录的按钮。

问题是当我按下删除按钮时,转发器中的一条记录被删除,整个页面的回发被触发。据我了解,只有更新面板内的部分应该刷新。

我还应该提到,在我的母版页上,我有一个脚本管理器,其属性“EnablePartialRendering”设置为 true

有人可以帮我解决这个问题吗?提前致谢。

ASP 文件:

<%@ 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 文件:

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

【问题讨论】:

  • 尝试将 UpdatePanel 放在中继器之外,而不是在 ContentTemplate 中。
  • 感谢您的建议。我只是尝试过,但遗憾的是它没有任何明显的效果。这可能与转发器位于用户控件中这一事实有关?

标签: asp.net updatepanel repeater


【解决方案1】:

在另一个 stackoverflow 帖子 (See Eugene S answer) 上找到了解决方案

您必须手动将控件添加到脚本管理器。您应该在 File Repeater 的 Item created 事件中执行此操作。祝你好运。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多