[英]AsyncPostBackTrigger Just flashing/flicking the UpdatePanel but not updating it
我正在尝试通过查找控件方法在母版页上尝试UpdatePanel&AsyncPostBackTrigger,但问题是当我单击按钮(UpdateButton)时,它只是闪烁/轻拂(没有回发)UpdatePanle,但仍然不更新或刷新其中的gridview(图像) updatePanel。
我已经将脚本管理器放在母版页上,并将Ager的“更新”面板放在子页的ContentPlaceHolder中。 另外,在另一个ContentPlaceholder中,有一个asp按钮(位于UpdatePanel之外)。
我想使用此asp按钮刷新/重新加载AJAX UpdatePanel。
感谢您的建议。
子页面代码:-
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>
嗨,更新代码 :-现在,单击事件会刷新整个页面。
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);
}
}
}
在母版页代码(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);
}
网络配置
<pages maintainScrollPositionOnPostBack="true" enableViewStateMac="true">
<controls>
<add tagPrefix="it" namespace="EProxy" assembly="App_Code"/>
</controls>
</pages>
之所以不起作用,是因为无法将母版页控件直接用作子页控件的AsyncPostBackTrigger。 但是,有一种替代方法可以通过代理工作。
首先,您需要创建以下类(将其放入与该类同名的单独的.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);
}
}
此类是一个控件,该控件将用于将单击事件从母版页代理到子页上,以刷新UpdatePanel。
创建控件后,在UpdatePanel之后添加以下内容:
<it:EventProxy runat="server" ID="ProxyControl" />
接下来,您需要向您的网站/ Web应用程序指示“ it:EventProxy”是什么。 为此,您需要通过将其添加到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>
在上面的示例中,将namespace属性的值设置为EventProxy类的命名空间,并将Assembly属性的值设置为解决方案的名称。
完成此操作后,将EventProxy控件的EventProxied事件作为AsyncPostBackTrigger添加到UpdatePanel。 您的标记应类似于以下内容:
<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" />
然后,在母版页按钮(用于刷新子页UpdatePanel)的单击事件内调用以下事件:
protected void btnRefresh_Click(object sender, EventArgs e)
{
EventProxy eventProxy = MasterPageContentPlaceHolder.FindControl("ProxyControl") as EventProxy;
eventProxy.ProxyEvent(e);
}
而已!
现在将发生的事情是,当您单击母版页中的按钮时,它将单击的事件代理到子页的EventProxy控件,这又将导致UpdatePanel刷新,因为EventProxy控件是其AsyncPostBackTriggers之一。
将您的按钮放在它自己的updatePanel或您要使用该按钮更新的同一updatePanel中。 完成此操作后,updatePanel将进行更新,而无需任何代码隐藏,单击该按钮将为所有更新面板中的所有控件调用异步回发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.