简体   繁体   中英

Javascript for running code-behind code when a button is clicked

i have a popup that is getting displayed when Save button is clicked. The popup has 2 buttons. Yes and No. No should cancel the popup and yes should take you to function in the code-behind say, btnSave_Click(object sender, Eventargs e). How is it possible. Could someone help me, i am new to Javascript.

Below is the code where i am showin the popup.

var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
     if(mdlPopup)
     {
       mdlPopup.show();           
     }

You can't call server side code from JavaScript directly. Make a postback or fire a XHR (AJAX) request in the background.

To do this you will need to set your server side function as a web method like this:

Add to the top of your code behind:

using System.Web.Services;
using System.Web.Script.Services;

Then decorate your method with these attributes:

 [WebMethod(), ScriptMethod()]
 public static void btnSave_Click(Object sender)
 {
   //Stuff
 }

To call this from the client side (Javascript) do this:

PageMethods.btnSave_Click(this,btnSave_Click_Finished);

You can place that in a client click event. The first argument is the sender parameter, the second is the javascript function to call when the server side method has completed.

I think it is possible for you to acess the serverside script by using javascript. __doPostBack is a function which is behind the asp postback and this function is called when a button is clicked or dropdown value is changed. For more details pls refer to [this.][1]

All you need is to place a <asp:Button ID="btnSave" runat="server" onClick="btnSave_Click" /> in the form and in the javascript (ie button click function) just call the __doPostBack('<%= btnSave.UniqueID %>', '');

So it will calls the serverside function.(btnSave_Click) Notice that you need to give '<%= btnSave.UniqueID %>' as the firstargument.

The above will works as similar to a server button click

The another way is to make a post or ajax using jquery.post or jquery.ajax it is possible to send request asynchronously to the server.Here you want to pass some query string and call the appropriate function in the page_Load This will do without any postback

One another method is to use PageMethods from clientside by defining a static WebMethod

[1]: http://msdn.microsoft.com/en-us/library/aa720099%28vs.71%29.aspx/ "Call serverside function from clientside" I hope any one of these will solve your problem

the Yes button should be an asp.net control with a server-side handler

markup

<asp:button id="yesButton" runat="server" onclick="yes_click" />

codebehind

void yes_click(object sender, EventArgs e) {
   // TODO your thing here.
}

the other buttons can be standard html input s with javascript event handlers. if you are using javascript to alter element style, the changes won't persist across a postback- when the yes button submits and the page reloads, the popup won't be visible.

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