简体   繁体   English

AsyncPostBackTrigger只是闪烁/滑动UpdatePanel而不更新它

[英]AsyncPostBackTrigger Just flashing/flicking the UpdatePanel but not updating it

I am trying UpdatePanel & AsyncPostBackTrigger on master pages through find control method but problem is when I click on button (UpdateButton) It just flash/flick (No Postback) the UpdatePanle but still it don't update or refresh the gridview (images) inside the updatePanel. 我正在尝试通过查找控件方法在母版页上尝试UpdatePanel&AsyncPostBackTrigger,但问题是当我单击按钮(UpdateButton)时,它只是闪烁/轻拂(没有回发)UpdatePanle,但仍然不更新或刷新其中的gridview(图像) updatePanel。

I have placed script Manger on the master page & an AJAX Update panel in a ContentPlaceHolder in the child page. 我已经将脚本管理器放在母版页上,并将Ager的“更新”面板放在子页的ContentPlaceHolder中。 Also, in another ContentPlaceholder there is an asp button (outside of the UpdatePanel ). 另外,在另一个ContentPlaceholder中,有一个asp按钮(位于UpdatePanel之外)。

I want to refresh/reload the AJAX UpdatePanel with this asp button. 我想使用此asp按钮刷新/重新加载AJAX UpdatePanel。

Thanks for suggestions. 感谢您的建议。

Child Page Code :- 子页面代码:-

protected void Page_Load(object sender, EventArgs e)
 {
        ScriptManager ScriptManager1 = (ScriptManager)Master.FindControl("ScriptManager1");
        ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("cp_Button");
        Button btnRefresh = (Button)cph.FindControl("btnRefresh");
        ScriptManager1.RegisterAsyncPostBackControl(btnRefresh);
 }
 protected void btnRefresh_Click(object sender, EventArgs e)
 {
        UpdatePanel1.Update();

 }         


<%@ Page Title="" Language="C#" MasterPageFile="~/InnerMaster.master" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="A" Async="true" %>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
             <Columns>
                 <asp:TemplateField>
                     <ItemTemplate>
                        <asp:Image ID="img12" runat="server" Width="650px" Height="600" ToolTip="A" ImageUrl='<%# Page.ResolveUrl(string.Format("~/Cli/{0}", Eval("image"))) %>' />
                     </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
           </asp:GridView>
         </ContentTemplate>
      </asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="cp_Button" Runat="Server">   
    <asp:Button ID ="btnRefresh" runat="server" onclick="btnRefresh_Click" Height="34" Width="110" Text="More Images" />
</asp:Content>

Hi updated code :- Now on click event whole pages is refreshed. 嗨,更新代码 :-现在,单击事件会刷新整个页面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;


    namespace EProxy
    {
        public class EventProxy : Control, IPostBackEventHandler
        {
            public EventProxy()
            { }

            public void RaisePostBackEvent(string eventArgument)
            { }

            public event EventHandler<EventArgs> EventProxied;

            protected virtual void OnEventProxy(EventArgs e)
            {
                if (this.EventProxied != null)
                {
                    this.EventProxied(this, e);
                }
            }

            public void ProxyEvent(EventArgs e)
            {
                OnEventProxy(e);
            }
        }
    }

On Master Page Code (btn click):- 在母版页代码(btn单击)上:-

protected void btnRefresh_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder cph = (ContentPlaceHolder)this.FindControl("MainContent");
        EventProxy eventProxy = (EventProxy)cph.FindControl("ProxyControl") as EventProxy;

        eventProxy.ProxyEvent(e);

    }

Web Config :- 网络配置

<pages maintainScrollPositionOnPostBack="true"  enableViewStateMac="true">
  <controls>
    <add tagPrefix="it" namespace="EProxy" assembly="App_Code"/>
  </controls>
</pages>

The reason that it doesn't work is because it's not possible to use a master page control directly as an AsyncPostBackTrigger for a child page control. 之所以不起作用,是因为无法将母版页控件直接用作子页控件的AsyncPostBackTrigger。 However, there is a workaround that works by means of a proxy. 但是,有一种替代方法可以通过代理工作。

First, you need to create the following class (Put it in a seperate .cs file with the same name as the class): 首先,您需要创建以下类(将其放入与该类同名的单独的.cs文件中):

public class EventProxy : Control, IPostBackEventHandler
{
    public EventProxy()
    { }

    public void RaisePostBackEvent(string eventArgument)
    { }

    public event EventHandler<EventArgs> EventProxied;

    protected virtual void OnEventProxy(EventArgs e)
    {
        if (this.EventProxied != null)
        {
            this.EventProxied(this, e);
        }
    }

    public void ProxyEvent(EventArgs e)
    {
        OnEventProxy(e);
    }
}

This class is a control that will be used to proxy the click event from the master page to the child page in order to refresh the UpdatePanel. 此类是一个控件,该控件将用于将单击事件从母版页代理到子页上,以刷新UpdatePanel。

After you created the control, add the following after your UpdatePanel: 创建控件后,在UpdatePanel之后添加以下内容:

<it:EventProxy runat="server" ID="ProxyControl" />

Next, you need to indicate to your website / web application what 'it:EventProxy' is. 接下来,您需要向您的网站/ Web应用程序指示“ it:EventProxy”是什么。 To do that you need indicate that it is a control by adding it to the <controls> tag in your web.config file: 为此,您需要通过将其添加到web.config文件的<controls>标记中来表明它是一个控件:

<pages maintainScrollPositionOnPostBack="true" theme="Default" enableViewStateMac="true">
        <controls>
            <add tagPrefix="it" namespace="The namespace that you saved the EventProxy class in" assembly="Your Assembly name"/>
        </controls>
</pages>

In the above exmaple, set the value of the namespace attribute to the namespace of the EventProxy class, and set the value of the assembly attribute to your solution's name. 在上面的示例中,将namespace属性的值设置为EventProxy类的命名空间,并将Assembly属性的值设置为解决方案的名称。

After you have done that, add the EventProxy control's EventProxied event as an AsyncPostBackTrigger to your UpdatePanel. 完成此操作后,将EventProxy控件的EventProxied事件作为AsyncPostBackTrigger添加到UpdatePanel。 Your markup should look something like the following: 您的标记应类似于以下内容:

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ProxyControl" EventName="EventProxied" />
        </Triggers>
    </asp:UpdatePanel>
    <it:EventProxy runat="server" ID="ProxyControl" />

Then, call the following inside of your master page's button (That will be used to refresh the child page UpdatePanel) click event: 然后,在母版页按钮(用于刷新子页UpdatePanel)的单击事件内调用以下事件:

protected void btnRefresh_Click(object sender, EventArgs e)
{
    EventProxy eventProxy = MasterPageContentPlaceHolder.FindControl("ProxyControl") as EventProxy;
    eventProxy.ProxyEvent(e);
}

That's it! 而已!

Now what will happen is when you click the button in the master page, it will proxy the event of that click to the child page's EventProxy Control, that will in turn cause the UpdatePanel to refresh since the EventProxy control is one of its AsyncPostBackTriggers. 现在将发生的事情是,当您单击母版页中的按钮时,它将单击的事件代理到子页的EventProxy控件,这又将导致UpdatePanel刷新,因为EventProxy控件是其AsyncPostBackTriggers之一。

Place your button in it's own updatePanel or in the same updatePanel that you want to update with that button. 将您的按钮放在它自己的updatePanel或您要使用该按钮更新的同一updatePanel中。 Once yo do that, the updatePanel will update without codebehind, the button click will call an async postback for all controls inside all update panels. 完成此操作后,updatePanel将进行更新,而无需任何代码隐藏,单击该按钮将为所有更新面板中的所有控件调用异步回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM