简体   繁体   English

单击按钮时用于运行代码隐藏代码的Javascript

[英]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. 弹出窗口有2个按钮。 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). 是和否。否应该取消弹出窗口,是应该使您在后面的代码btnSave_Click(object sender,Eventargs e)中起作用。 How is it possible. 这怎么可能。 Could someone help me, i am new to Javascript. 有人可以帮助我,我是Java语言的新手。

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. 您不能直接从JavaScript调用服务器端代码。 Make a postback or fire a XHR (AJAX) request in the background. 进行回发或在后台触发XHR(AJAX)请求。

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: 要从客户端(JavaScript)调用此操作,请执行以下操作:

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. 第一个参数是sender参数,第二个参数是服务器端方法完成后要调用的javascript函数。

I think it is possible for you to acess the serverside script by using javascript. 我认为您可以使用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. __doPostBack是位于asp postback后面的函数,当单击按钮或更改下拉值时,将调用此函数。 For more details pls refer to [this.][1] 有关更多详细信息,请参考[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 %>', ''); 您只需要在表单和javascript(即按钮单击功能)中放置<asp:Button ID="btnSave" runat="server" onClick="btnSave_Click" /> ,只需调用__doPostBack('<%= btnSave.UniqueID %>', '');

So it will calls the serverside function.(btnSave_Click) Notice that you need to give '<%= btnSave.UniqueID %>' as the firstargument. 因此它将调用服务器端函数。(btnSave_Click)注意,您需要将'<%= btnSave.UniqueID %>'作为第一个参数。

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 另一种方法是使用jquery.postjquery.ajax来发布帖子或ajax,可以将请求异步发送到服务器。在这里,您想传递一些查询字符串并在page_Load调用适当的函数。回传

One another method is to use PageMethods from clientside by defining a static WebMethod 另一种方法是通过定义静态WebMethod从客户端使用PageMethods

[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 [1]: http : //msdn.microsoft.com/zh-cn/library/aa720099%28vs.71%29.aspx/ “从客户端调用服务器端功能”我希望其中任何一个都能解决您的问题

the Yes button should be an asp.net control with a server-side handler Yes按钮应该是带有服务器端处理程序的asp.net控件

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. 其他按钮可以是带有javascript事件处理程序的标准html input 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. 如果您使用JavaScript来更改元素样式,则更改将不会在回发中持续存在-当提交yes按钮并重新加载页面时,将不会显示弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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