NET导航在code锚背后

NET导航在code锚背后

本文介绍了ASP.NET导航在code锚背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code这样一个简单的Web窗体:

I have a simple Web Form with code like this:

//...
//tons of text
//...
<a name="message" />
//...
//tons of text
//...
<asp:Button ID="ButtonSend"
                runat="server"
                text="Send"
                onclick="ButtonSend_Click" />

POST之后,我要导航用户到我的主播消息。我有以下code此:

After POST I want to navigate user to my anchor "message". I have following code for this:

protected void ButtonSend_Click(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.location.hash='#message';",
                                        true);
}

这简单的JavaScript是不是在Firefox 3.5.2工作 - 网址在浏览器中改变,但网页无法浏览到锚。在IE 8中它完美的作品。

This simple JavaScript is not working in Firefox 3.5.2 - url is changing in browser but page is not navigated to anchor. In IE 8 it works perfectly.

为什么这段JavaScript code未在Firefox工作?我缺少的东西吗?

Why this JavaScript code is not working in Firefox? Am I missing something?

推荐答案

我已经解决了我的问题。 JavaScript的code调用之前我锚甚至存在。这就是为什么火狐不滚动翻页。

I've solved my problem. JavaScript code was called before my anchor even existed. That's why Firefox wasn't scroll page down.

我的code现在看起来像tihs:

My code looks now like tihs:

this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.onload = function() {window.location.hash='#message';}",
                                         true);

页面加载后,我打电话给我的简单的JavaScript函数。

After page load I'm calling my simple JavaScript function.

要找到解决方案的关键是里贝罗回答。 Firebug的报告说的getElementById被返回null引用。然后我看着生成的HTML像andrewWinn建议 - 锚之前存在的JavaScript调用。取得什么谷歌搜索和发现解决方案。

The key to found solution was Cleiton answer. Firebug reports that getElementById was returning null reference. Then I looked at generated HTML like andrewWinn suggested - JavaScript was called before anchor existed. Made little googling and found solution.

谢谢您的回答!

这篇关于ASP.NET导航在code锚背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:49