ASP.NET导航到后面的代码锚点

GTD*_*GTD 3 javascript asp.net anchor

我有一个简单的Web窗体,代码如下:

//...
//tons of text
//...
<a name="message" />
//...
//tons of text
//...
<asp:Button ID="ButtonSend" 
                runat="server" 
                text="Send"
                onclick="ButtonSend_Click" />
Run Code Online (Sandbox Code Playgroud)

POST后我想导航用户到我的主播"消息".我有以下代码:

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

这个简单的JavaScript在Firefox 3.5.2中不起作用 - url在浏览器中更改但页面未导航到锚点.在IE 8中它完美运行.

为什么这个JavaScript代码在Firefox中不起作用?我错过了什么吗?

GTD*_*GTD 7

我已经解决了我的问题.在我的锚点存在之前调用了JavaScript代码.这就是Firefox没有滚动页面的原因.

我的代码现在看起来像tihs:

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

页面加载后,我正在调用我的简单JavaScript函数.

找到解决方案的关键是克莱顿回答.Firebug报告getElementById返回null引用.然后我查看生成的HTML,就像andrewWinn建议的那样 - 在锚点存在之前调用了JavaScript.做了一点谷歌搜索并找到了解决方案.

谢谢你的回答!


Cle*_*ton 5

门多萨,您可以使用本机的scrollIntoView函数。

做你想做的,只要写:

this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "document.getElementById('id_of_the_element').scrollIntoView();",
                                        true);
Run Code Online (Sandbox Code Playgroud)