【问题标题】:UpdatePanel triggers not firingUpdatePanel 触发器未触发
【发布时间】:2015-02-05 19:53:12
【问题描述】:

我对 UpdatePanel(ASP.Net WebForms、.Net 4.0)有疑问。这是代码:

        <div class="container-fluid">
        <form id="form1" runat="server">
            <h2>Poruke</h2>
            <div class="row">
                <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
                <asp:UpdatePanel ID="msgListUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
                    <ContentTemplate>
                        <div class="col-md-4">
                            <asp:ListBox ID="msgList" runat="server" OnSelectedIndexChanged="msgList_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="AutoID"></asp:ListBox>
                        </div>
                        <div class="col-md-8">
                            <asp:ListBox ID="conversationList" runat="server" ClientIDMode="AutoID"></asp:ListBox>
                            <br class="divider" />
                            <p>
                                Odgovor: <span>
                                    <asp:TextBox ID="replyTxtbox" runat="server"></asp:TextBox></span>
                            </p>
                            <asp:Button ID="sendBtn" runat="server" Text="Pošalji" OnClick="sendBtn_Click" EnableViewState="false" ClientIDMode="AutoID" />
                        </div>
                    </ContentTemplate>
                <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="msgList" EventName="SelectedIndexChanged"/>
                    </Triggers>
                </asp:UpdatePanel>
            </div>
        </form>
    </div>    

这是代码隐藏...

    int userIdCookie = 0;
    message selected = new message();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.User.Identity.IsAuthenticated)
        {
            FormsAuthentication.RedirectToLoginPage();
        }

        if (!Page.IsPostBack)
        {
            if (Int32.TryParse(HttpContext.Current.User.Identity.Name, out userIdCookie))
            {
                message msg = new message();
                var allMsg = msg.allMessagesFormatted().Distinct().ToList();
                msgList.DataSource = allMsg;
                msgList.DataBind();
            }
        }
        else
        {
            // test only!
            replyTxtbox.Text = msgList.SelectedIndex.ToString();
            msgListUpdatePanel.Update();
        }
    }

    protected void msgList_SelectedIndexChanged(object sender, EventArgs e)
    {
        message msg = new message();

        var allMsg = msg.allMessagesFormatted().Distinct().ToList();
        msgList.DataSource = allMsg;

        IList<message> boundList = (IList<message>)msgList.DataSource;

        selected = boundList[msgList.SelectedIndex];
        var conversation = msg.allMessagesFormatted().FindAll(x => x.conversationGuid == selected.conversationGuid);

        conversationList.DataSource = conversation;
        conversationList.DataBind();
    }

    protected void sendBtn_Click(object sender, EventArgs e)
    {
        if(selected.recipientId != 0)
        {
            message newmsg = new message();
            newmsg.senderId = userIdCookie;
            newmsg.recipientId = selected.recipientId;
            newmsg.subject = selected.subject;
            newmsg.messageTxt = replyTxtbox.Text;
            newmsg.conversationGuid = selected.conversationGuid;
            newmsg.time = DateTime.Now;
            newmsg.Add();
        }
    }    

msgList 填充得很好,但是当我更改选择时,什么也没有发生 - 它的 SelectedIndex 事件永远不会触发。如果我将 AutoPostBack="true" 设置为此列表框,它会重新加载页面(这正是我想要避免的)。

总而言之 - 当我单击 UpdatePanel 内的 ListBox 中的项目时,没有任何反应(未触发事件)。我想避免在选定索引更改时重新加载页面。我尝试了十几种解决方案(ClientID、AsyncPostBack、“常规”PostBack 触发器,我想我错过了一个简单的细节,这让我发疯了。

谁能帮忙?

编辑 - 正如@mason 指出的那样,问题出在包含\r\n 字符的覆盖message.ToString() 方法中,导致回发出现问题。

【问题讨论】:

    标签: c# asp.net ajax


    【解决方案1】:

    您将在浏览器的控制台中收到 JavaScript 错误。

    未捕获的 Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:回发无效 或回调参数。使用配置或 在页面中。出于安全目的, 此功能验证回发或回调事件的参数 源自最初呈现它们的服务器控件。如果 数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法,以便 注册回发或回调数据以进行验证。 MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1 未捕获 Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:回发无效 或回调参数。使用配置或 在页面中。出于安全目的, 此功能验证回发或回调事件的参数 源自最初呈现它们的服务器控件。如果 数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法,以便 注册回发或回调数据以进行验证。

    如果你使用,你可以看到一个更简单的版本:

    msgList.DataSource = new List<string>(){"A\r\n","B\r\n","C\r\n"};
    

    当您在浏览器选项卡中观看时,您会看到 POST 请求已发送到服务器,但在服务器端,Page_Load 方法根本不会被命中。

    解决方法是在用于 ListBox 的数据中不使用 \r\n 字符,或者按照说明在 ClientScriptManager.RegisterForEventValidation on MSDN 注册它。

    【讨论】:

    • 难以置信。当我添加 msgList.DataSource= new List&lt;string&gt;(){"A","B","C"}; 并且问题出在 Message 类中被覆盖的 .ToString() 时,它工作得很好。我将\r\n 放入方法中,但有些东西不喜欢那样。当我删除它时,其他一切都很好。问题解决了!
    • @nighthawk 我更新了解决方案。无论如何,我建议永远不要使用 UpdatePanel。相反,改用ASP.NET Web API 和客户端脚本框架(如jQuery),使AJAX 变得简单。
    • 我可能会将其切换到 WebAPI,因为 UpdatePanel 会导致很多问题(例如这个)并且会减慢页面速度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多