C# asp:asp:超链接中的按钮无法导航到 Internet Explorer 中的页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp:Button inside asp:hyperlink does not navigate to page in internet explorer
提问by Xaisoft
I have an asp:button which is inside an asp:hyperlink. When you click the button in firefox, it goes to the correct url specified by the asp:hyperlink, but if you click the button in internet explorer, it stays on the same page. I am just using the PostBackUrl property on the button to resolve the issue, but here is an example of the code:
我有一个 asp:button,它位于 asp:hyperlink 内。当您在 firefox 中单击该按钮时,它会转到由 asp:hyperlink 指定的正确 URL,但是如果您在 Internet Explorer 中单击该按钮,它会停留在同一页面上。我只是使用按钮上的 PostBackUrl 属性来解决问题,但这里是代码示例:
<asp:Hyperlink ID="hyp" runat="server" NavigateUrl="Page2.aspx">
<asp:Button ID="btn" runat="server" Text="Submit" /></asp:Hyperlink>
Why does the above work in firefox, but not IE?
为什么上述内容在 Firefox 中有效,而在 IE 中无效?
采纳答案by Mehrdad Afshari
What you did is not very correct.
你所做的不是很正确。
Just add the button and in its click handler do:
只需添加按钮并在其点击处理程序中执行以下操作:
Response.Redirect("Page2.aspx");
Alternatively you can write a line of javascript:
或者,您可以编写一行 javascript:
<input type="button" value="Text" onclick="location='Page2.aspx'" />
回答by TheTXI
Your button is executing a postback on the same page. If I were you I would try and use a standard button in your HTML toolbox instead of an ASP.NET button.
您的按钮正在同一页面上执行回发。如果我是你,我会尝试在 HTML 工具箱中使用标准按钮而不是 ASP.NET 按钮。
Edit: I would more highly suggest eliminating your button control and styling your hyperlink appropriately, or eliminate both and use a hyperlinkbutton control instead and have its display properties set to display as a button.
编辑:我更强烈建议消除您的按钮控件并适当地设置超链接的样式,或者消除两者并改用超链接按钮控件并将其显示属性设置为显示为按钮。
回答by bendewey
Is there a reason why you are using a button inside a hyperlink? Depending on the design you are trying to achive I would use just a Button or a LinkButton and then do a redirect after your logic in the codebehind
您是否有理由在超链接中使用按钮?根据您尝试实现的设计,我将只使用 Button 或 LinkButton,然后在代码隐藏中的逻辑之后进行重定向
<asp:Button runat='server' id='button1' Text='Click Me' Click='button1_Click' />
<asp:LinkButton runat='server' id='linkbutton1' Text='Click Me' Click='button1_Click' />
Code-Behind
代码隐藏
protected void button1_Click(object sender, EventArgs e) {
// some logic
Response.Redirect("Page2.aspx");
}
Firefox vs Internet Explorer
Firefox 与 Internet Explorer
I suspect your having discrepencies between Firefox and Internet Explorer because of the way the events are bubbled/propogated between the browsers. If you would like to cancel the propagation of the event, you would need to include a call to event.preventDefault() or event.stopPropagation() in your button click event handler (in javascript)?
由于事件在浏览器之间冒泡/传播的方式,我怀疑您在 Firefox 和 Internet Explorer 之间存在差异。如果您想取消事件的传播,您需要在按钮单击事件处理程序(在 javascript 中)中包含对 event.preventDefault() 或 event.stopPropagation() 的调用?
回答by Nick Allen
I would use an <asp:LinkButton CssClass="myButton" OnClick="Redirect" />
and then CSS to style it how you want it to look and the code-behind to handle the functionality.
我会使用一个<asp:LinkButton CssClass="myButton" OnClick="Redirect" />
然后 CSS 来设置它的样式,你想要它的外观和代码隐藏来处理功能。
protected void Redirect(object sender, EventArgs e)
{
// do something
}
Three way seperation of presentation, markup and functionality is the way to go in my opinion
在我看来,演示、标记和功能的三种分离方式是要走的路