ASP 中的按钮,通过 Javascript 在 C# 文件中调用
我有一个 asp.net 页面,它具有以下属性
<asp:Button ID="btnBack" runat="server" CssClass="Button" Text="< Back to Home" /></td>
在 C# 代码文件中我正在执行类似的
btnBack.Attributes.Add("onclick", "javascript:history.go(-1);return false");
操作 问题是它并不总是让我返回主页,而是返回其他页面..
无论如何我可以添加例如,在此功能中,我想要导航的页面(我知道它是错误的,但请看看我想要实现的目标)
btnBack.Attributes.Add("onclick", "~/Home.aspx");
类似的东西,
我将感谢您的帮助
I have a asp.net page where it has the following properties
<asp:Button ID="btnBack" runat="server" CssClass="Button" Text="< Back to Home" /></td>
And in the C# code file I am doing something like this
btnBack.Attributes.Add("onclick", "javascript:history.go(-1);return false");
The problem is it is not always returning me to the Home page but some other page..
Is there anyway I can add in this function the page where I want to navigate for instance (and I know its wrong but please see what i want to achieve)
btnBack.Attributes.Add("onclick", "~/Home.aspx");
Something like that
I would appreciate your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您并不总是获得返回主页的行为的原因是
history.go(-1)
只是告诉浏览器导航回加载的上一个页面。它可能并不总是主页。javascript 的一个简单解决方案是更改 location 属性,这将触发浏览器加载提供的 URL。
OnClientClick 会将 javascript 字符串附加到 html 标记中的 onclick 事件。在我看来,这比类似的东西更容易、更干净
btnBack.Attributes.Add("onclick", "window.location=~/Home.aspx"
The reason you aren't always getting the behavior of moving back to the home page is because
history.go(-1)
just tells the browser to navigate back to the previous page that was loaded. It may not always be the home page.A simple solution from javascript is to change the location property which will trigger the broswer to load the URL supplied
The OnClientClick will attach the javascript string to the onclick event in html markup for you. This is easier and cleaner IMO than something like
btnBack.Attributes.Add("onclick", "window.location=~/Home.aspx"