vb.net 停止页面重新加载 ASP.NET 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16372431/
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
Stop page reload of an ASP.NET button
提问by user2217039
NET application, I have inserted a button that call a Javascript function (OnClientClickevent) and a VB.NET function (OnClickevent)
NET 应用程序,我插入了一个调用 Javascript 函数(OnClientClick事件)和 VB.NET 函数(OnClick事件)的按钮
<asp:Button OnClientClick="jsfunction() " OnClick="vbfunction" Text="Submit" runat="server" />
The problem is that when I click the button, it refreshes the page and delete the content of the text boxes.
问题是当我单击按钮时,它会刷新页面并删除文本框的内容。
I have tried with inserting return false;on the OnClienClickevent, but it doesn't execute the OnClick Event.
我尝试return false;在OnClienClick事件上插入,但它不执行 OnClick 事件。
How can I avoid the page reload ?
如何避免页面重新加载?
P.S.: At the end of the Javascript function a new window is opened window.open(newWindow.aspx), but I want that the first page mantain the value inserted by the user in the Text Boxes.
PS:在 Javascript 函数的末尾会打开一个新窗口window.open(newWindow.aspx),但我希望第一页保留用户在文本框中插入的值。
Thanks in advance :)
提前致谢 :)
回答by Adil
You need to use return statement at two points.
您需要在两点使用 return 语句。
OnClientClick="return jsfunction();"
function jsfunction()
{
//
return false;
}
OR, you can return false after the function call like this.
或者,您可以像这样在函数调用后返回 false。
OnClientClick="jsfunction(); return false;"
Noteif you want to do postback conditionally then you need to return true or false.
请注意,如果您想有条件地进行回发,则需要返回 true 或 false。
OnClientClick="return jsfunction();"
function jsfunction()
{
if(conditionForPostBack)
return true;
else
return false;
}
回答by Ujjwal Manandhar
or you can disable the submit behaviour. By default asp.net renders button as submit button. if you disable submit behaviour it will render button as button type
或者您可以禁用提交行为。默认情况下,asp.net 将按钮呈现为提交按钮。如果您禁用提交行为,它会将按钮呈现为按钮类型
<asp:Button UseSubmitBehavior="false" OnClientClick="jsfunction() " OnClick="vbfunction" Text="Submit" runat="server" />
But with this code it will not fire server side event "OnClick"
但是使用此代码,它不会触发服务器端事件“OnClick”
回答by Arif YILMAZ
if you are not going to trigger the button with C# Codebehind function, then you dont need to use asp:Button. Therefore you can use a regular html .
如果您不打算使用 C# Codebehind 函数触发按钮,则不需要使用 asp:Button。因此,您可以使用常规 html 。
<button id='btn_1' onclick='ajax_function()'>Button</button>
html button is much easier and faster. if you use asp:button, then you should use clientid() function to catch the control to trigger the ajax.
html 按钮更容易和更快。如果您使用asp:button,那么您应该使用clientid() 函数来捕获控件以触发ajax。
回答by Sergio Gutiérrez
Searching for the same thing as you i find a patch:
搜索与您相同的东西,我找到了一个补丁:
If you call a method server side, you can use AJAXwith the update panel, but that didn't worked for me. But you can save what you want in Session, so you have it as far as Session lasts.
如果您调用方法服务器端,则可以AJAX与更新面板一起使用,但这对我不起作用。但是您可以在 中保存您想要的内容Session,因此只要 Session 持续,您就可以拥有它。
// Save at SessionParameter the elementToSave we want.
this.Session["SessionParameter"] = elementToSave;
// Retrieve the info from the Session
ElementYouNeededToSave = (TypeOfTheElement)Session["SessionParameter"];
Hope this will help someone in my situation.
希望这会对我这种情况的人有所帮助。

