简体   繁体   中英

Prevent jquery colorbox closing on postback

I am using jquery colorbox to call an external page from codebehind like this

 String URL = "Default.aspx"; 
 ScriptManager.RegisterStartupScript(this, this.GetType(), "openWindow", "$.colorbox({ href: '" + URL + "', closeButton: false, overlayClose: false });", true);

Below is html of Default.aspx

<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
</div>
</form>
</body>

Currently nothing is written on button click event, but as soon as I submit the button, the model window is getting closed. How do I prevent this? I want to be able to do certain calculation codebehind and then close the modelwindow if everything is fine.

Thanks,

Vinay

Your button click submits the form and that executes a postback. If you need to perform server-side logic, you will need to get your button to call an ajax method to get the results from the server. Do do this, you either replace the OnClick with an OnClientClick method to call your ajax or you use a normal button and you wire up the on click event in javascript.

HTML

 <asp:Button ID="btn" runat="server" Text="Submit" OnClientClick="btn_Click" />

Javascript Something along these lines:

function btn_Click(e){
    e.preventDefault();
    //call your ajax here
    //update the page on ajax's success event
}

This should do it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM