从 JavaScript 调用 ASP.NET EventHandler

发布于 2024-12-29 20:06:57 字数 666 浏览 3 评论 0原文

我有一个传统的 ASP.NET Web 表单。我们曾经有一个按钮定义如下:

<asp:Button ID="myButton" Runat="server" OnClick="myButton_Click" />

在 .aspx.cs 页面中,我有以下内容:

protected void myButton_Click(object sender, EventArgs e)
{
  // Do Stuff
}

但是,现在有人决定使用一个奇特的 JavaScript 框架。本质上,我只是:

  <input type="button" id="myButton" onclick="someJSFunction()" />
  <script type="text/javascript">
    function someJSFunction() {
      // Need to execute "myButton_Click" here.
    }
  </script>

如何从这里在服务器上实际执行我的原始方法?我知道这是一个非常奇怪的问题。我已经简化了代码,以尝试直接传达我想要完成的任务。有人可以告诉我该怎么做吗?

太感谢了!

I have a traditional ASP.NET Web Form. We used to have a button defined as follows:

<asp:Button ID="myButton" Runat="server" OnClick="myButton_Click" />

In the .aspx.cs page, I have the following:

protected void myButton_Click(object sender, EventArgs e)
{
  // Do Stuff
}

However, now someone has decided to use a fancy JavaScript framework. Essentially, I just have:

  <input type="button" id="myButton" onclick="someJSFunction()" />
  <script type="text/javascript">
    function someJSFunction() {
      // Need to execute "myButton_Click" here.
    }
  </script>

How do I actually execute my original method on the server from here? I know this is a very bizarre question. I've simplified the code to try to and be a direct in communicating what I'm trying to accomplish. Can someone please tell me how to do this?

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烦人精 2025-01-05 20:06:57

您可以使用 ASP.Net ClientScriptManager 编写

__doPostBack('<%=myButton.ClientID%>','OnClick');

,或者使用 ASP.Net ClientScriptManager 编写,或者更好一点且不易出错。

<%= Page.ClientScript.GetPostBackEventReference(myButton, String.Empty) %>; 

第二个实际上是在幕后编写 __doSubmit ,但您可以让 ASP.Net 框架为您完成此操作。此外,第一个将回发页面,但第二个也会触发正确的服务器端事件。它更好地集成到 ASP.Net 架构中。不过,两者都可以。

最近的这个SO问题 比我更好地解释了这一切(或者说当时)

You could write in

__doPostBack('<%=myButton.ClientID%>','OnClick');

or a bit better and less error prone use the ASP.Net ClientScriptManager

<%= Page.ClientScript.GetPostBackEventReference(myButton, String.Empty) %>; 

The second actually writes __doSubmit under the covers but you are getting the ASP.Net framework to do it for you. Also th efirst one will post back the page but the second triggers the correct server side events as well. It integrates a lot better into the ASP.Net architecture. Either would work though.

This recent SO question explains it all far better than I can (or rather did at the time)

止于盛夏 2025-01-05 20:06:57

试试这个:

<input type="button" id="myButton" onclick="someJSFunction(); __doPostBack('myButton','OnClick');" />

try this:

<input type="button" id="myButton" onclick="someJSFunction(); __doPostBack('myButton','OnClick');" />
祁梦 2025-01-05 20:06:57

首先,无论您的按钮在做什么,执行该功能的实际代码都不应该位于按钮后面的代码中;它应该位于由按钮后面的代码调用的方法中。按钮的事件处理程序决不能由按钮以外的任何对象直接使用。因此,您的按钮处理程序应如下所示:

protected void Button1_Click(object sender, EventArgs e)
{
   CallMyRealMethod();
}

接下来,查看 SignalR。 (http://signalr.net) SignalR(简而言之)允许您从 javascript 调用 C# 方法,并且 javascript来自 C# 的方法。

使用这两种技术可以使您的代码更具可读性和可维护性。

First of all, whatever your button is doing, the actual code to do that function shouldn't be in the code behind the button; it should be in a method that is called by the code behind the button. The button's event handlers should never be used directly by any object other than the button. So your button handler should look like this:

protected void Button1_Click(object sender, EventArgs e)
{
   CallMyRealMethod();
}

Next, check out SignalR. (http://signalr.net) SignalR (in a nutshell) allows you to call C# methods from your javascript, and javascript methods from C#.

Using these two techniques can make your code much more readable and maintainable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文