Do something javascript before asp.net anypostback

纵饮孤独 提交于 2019-12-03 06:02:07
Emech

I find the way, in the asp.net forums and it was a some code in codebehind.

Just add this to your Page_Load event handler, changing the javaScript string to what you want to happen.

string scriptKey = "OnSubmitScript";
string javaScript = "alert('RegisterOnSubmitStatement fired');";
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), scriptKey, javaScript);

If you want to do something client-side before asp's postback, try using the OnClientClick attribute of the asp:button, eg:

<asp:Button OnClick="submit" OnClientClick="myPrePostbackFunction()" Text="Submit" runat="server" ... />

try:

$('form').submit(function () {
    return confirm('you sure?');
});

The form won't be submitted unless you return true and before that you can do all you want. It doesn't have to be a confirm() call of course.

try this?

    <form name="someform">
    <input type="submit" value="" />
</form>
<script type="text/javascript">
document.someform.onsubmit = function  ( e ) {
    e = e|| window.event;
    if ( !confirm ( "sure?") ){
        e.returnValue ? ( e.returnValue = false ):  e.preventDefault();
    }
}
// then jquery version
$("form").bind("submit", function  ( e ) {
    if ( !confirm ( "sure?" ) ){
        e.preventDefault();
    }
})
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!