问题描述
我有一个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.
推荐答案
"[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时避免页面刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!