如何在提交点击asp .net时避免页面刷新? [英] How to avoid page refresh on submit click in asp .net?

查看:85
本文介绍了如何在提交点击asp .net时避免页面刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个asp .net提交按钮。单击该按钮时,文本框和下拉列表中的值将插入到sql数据库中,它可以正常工作。但问题是,在单击提交按钮时,页面将刷新并插入值。为了避免页面刷新,我使用了onclientclick =return false,此时页面没有刷新但是值插入无法完成。

Hi,
I have an asp .net submit button. On clicking that button the values in the text box and drop down list are inserted to the sql database, it works fine. But the problem is that on clicking submit button the page refreshes and the values are inserted. To avoid page refresh I have used onclientclick="return false", at this time the page do not refreshes but the value insertion could not been done.

推荐答案

实际上它有很多方法,但让我告诉你一种方法。在这里我将使用Jquery。

首先创建JS函数或Jquery函数,它需要调用OnClientClick。比你必须调用Code Behind方法进行数据库操作(如果你有这样的方法,那就创建这样的)。

请记住,方法应为
Practically it has many ways, but let me show you 1 way to do so. Here i am going to use Jquery.
First of all create JS function or Jquery function, which required to call on "OnClientClick". Than you have to call the Code Behind method for doing the database operations(If you have such method else create such).
Remember that the method should be
"[WebMethod]"



考虑到函数名称JqueryFunc以便更好地理解。



.
Let consider function name "JqueryFunc" for better understanding.

<script type="text/javascript">
function JqueryFunc(){
    // Validate your controls if required.
    // Now you need to call the code behind method such like below & pass required parameters
    // consider parameters as other controls data which you required to store in DB.
    PageMethods.SetAllValues(parameter1,parameter2);
}
</script>





现在考虑你已经在代码中创建了方法,后面带参数并且需要在数据库中进行操作。



Now consider that you have created method in code behind which takes parameters and do required operation in database.

 [WebMethod]
//Consider parameters as per your requirement
 public static void SetAllValues(int parameter1, int parameter2)
{
  //perform all required database operations here.
}





现在只需从按钮调用JS函数。



And now just call the JS function from the button.

// OnClientCode will have the JS function and return false.
<asp:button runat="server" id="btnFunc" text="Func" onclientclick="JqueryFunc(); return false;"></asp:button>





希望这会帮助你。



Hope this will help you.


如何避免页面刷新后按钮点击事件在asp-net [ ^ ]



如何停止页面刷新 - 点击后按钮在asp-net [ ^ ]



跳它会帮助你..
how-to-avoid-page-refresh-after-button-click-event-in-asp-net[^]

how-to-stop-the-page-refresh-after-click-on-button-in-asp-net[^]

Hope it will help you..


一旦你将记录重定向提交到同一页面



http://www.aspsnippets.com/Articles/Avoid-duplicate-record- insert-on-page-refresh-in-ASPNet.aspx [ ^ ]
Once you submit the Records Redirect to the Same Page

http://www.aspsnippets.com/Articles/Avoid-duplicate-record-insert-on-page-refresh-in-ASPNet.aspx[^]


这篇关于如何在提交点击asp .net时避免页面刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆