ASP.NET回发失去哈希的URL [英] ASP.NET postbacks lose the hash in the URL

查看:162
本文介绍了ASP.NET回发失去哈希的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一个TabStrip一个ASP.NET页面,我使用的URL哈希code来跟踪我在(使用的 BBQ jQuery插件)。例如:

On an ASP.NET page with a tabstrip, I'm using the hash code in the URL to keep track of what tab I'm on (using the BBQ jQuery plugin). For example:

http://mysite.com/foo/home#tab=budget

不幸的是,我刚刚意识到,有几个页面上的地方,我使用的是老式的ASP.NET回发做的东西,并且回传完成后,哈希消失:

Unfortunately, I've just realized that there are a couple of places on the page where I'm using an old-fashioned ASP.NET postback to do stuff, and when the postback is complete, the hash is gone:

http://mysite.com/foo/home

...所以我接走到不同的标签。不好。

... so I'm whisked away to a different tab. No good.

这是使用.NET 4.0的web表单网站(不MVC)。正如你所看到的,不过,我使用的URL路由。

This is a webforms site (not MVC) using .NET 4.0. As you can see, though, I am using URL routing.

有没有办法告诉ASP.NET保持哈希在以下网址回发?

Is there a way to tell ASP.NET to keep the hash in the URL following a postback?

推荐答案

的问题是,回发转到当前页面,这是在页面的表单中的作用设置的URL。默认情况下此URL没有#hash在asp.net中,其自动设置asp.net,你有没有对其进行控制。

The problem is that the postback goes to the url of the current page, which is set in the action of the form on the page. By default this url is without #hash in asp.net, and its automatically set by asp.net, you have no control over it.

您可以在#hash增加使用JavaScript形式action属性:

You could add the #hash to the forms action attribute with javascript:

document.getElementById("aspnetForm").action += location.hash

,或者,如果它已经更新了哈希一个动作:

or, if updating an action with a hash already in it:

var form = document.getElementById("aspnetForm");
form.action = form.action.split('#')[0] + location.hash

只是确保你在window.load执行此code和你的目标正确的ID

just make sure you execute this code on window.load and you target the right ID

这篇关于ASP.NET回发失去哈希的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆