ASP.NET Ajax 历史记录无法正常工作
我有一个 ASP.NET/C# 应用程序。
该应用程序在 UpdatePanel 内包含一个 PlaceHolder。
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click"/>
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
基本上,我根据使用函数单击的 LinkButton 来加载占位符中的控件,
LoadControls("ControlName");
我希望实现用户使用浏览器后退按钮的功能 以便通过控件进行导航。所以我像这样使用 AJAX 历史记录:
我将当前控件 ID 保存在会话变量中
Session["current"]
我在脚本管理器中启用历史记录并添加事件处理程序
<asp:ScriptManager ID="ScriptManager" runat="server" EnableHistory="True" onnavigate="ScriptManager_Navigate" />
(事件处理程序将在下面介绍) 首先,我将在 Linkbuttons 的事件处理程序中创建一个新的历史记录点(它们都使用相同的处理程序),
string ControlId=Session["current"].ToString();
if (ScriptManager.IsInAsyncPostBack && !ScriptManager.IsNavigating)
{
ScriptManager.AddHistoryPoint("Hist", ControlId);
}
然后在 Scriptmanager 导航处理程序中
protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
{
string Controlid = "";
if (e.State["index"] != null)
{
Controlid = e.State["Hist"].ToString();
LoadControls(Controlid );
}
}
创建一个新的历史点。当我加载控件时,我可以前后导航,一切都正常工作正常
这是问题:
1)第一个问题
我第一次点击“返回”。历史记录返回2步,而不是一步(此后它可以正常工作。)就像最后一条历史记录没有保存一样。
2)第二个问题
在“返回”到第一步之后,我可以做一个额外的“返回”,其中 e.State[“Hist”] 将为空并给我一个错误。当 e.State["Hist"] 为空时,如何禁用“浏览器后退”按钮?
非常感谢您的帮助。我希望我说清楚了。
如果您需要更多信息/代码,我很乐意提供
I have an ASP.NET/C# application.
The application containsa PlaceHolder inside an UpdatePanel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click"/>
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Basically I load controls in the placeholder depending on what LinkButton has been clicked using a function
LoadControls("ControlName");
I want to implement the ability for the user to use the Browser Back Button
in order to navigate through the controls. so I used AJAX History like this:
I save the current control ID in a session variable
Session["current"]
I enable History in scriptmanager and add an event handler
<asp:ScriptManager ID="ScriptManager" runat="server" EnableHistory="True" onnavigate="ScriptManager_Navigate" />
(The event handler will be covered below)
First I will create a new history point in the event handler of the Linkbuttons (they all use the same handler)
string ControlId=Session["current"].ToString();
if (ScriptManager.IsInAsyncPostBack && !ScriptManager.IsNavigating)
{
ScriptManager.AddHistoryPoint("Hist", ControlId);
}
then in the Scriptmanager navigate handler
protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
{
string Controlid = "";
if (e.State["index"] != null)
{
Controlid = e.State["Hist"].ToString();
LoadControls(Controlid );
}
}
When I load the controls I can navigate back and forward and everything is working fine
Here is the problem:
1) first problem
The first time I click on "Back". the history returns 2 steps and not one (after that it works normaly.) It is like the last history is not saved.
2) second problem
After "Backing" to the first step I can do an extra "Back" where the e.State["Hist"] will be null and give me an error. how can I disable the "Browser Back" button when the e.State["Hist"] will be null?
Thank you very much for any help. I hope I was clear.
If you need more info/code I will gladly provide them
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。这只是一个错误。 Session["Current"] 正在保存前一个控件,而不是当前控件。现在已经修复了。如果有人需要一个创建ajax历史记录的小例子,我会留下这个问题。
Never mind. it was just a bug. the Session["Current"] was saving the previous control and not the current one. It is now fixed. I will leave this question if somebody needs a small example for creating ajax history.