简体   繁体   English

从Javascript调用ac#方法

[英]Call a c# method from Javascript

Hi i want to call a C# method.. I already tryed with webmethod, but in the c# method i will not have acces to textbox or others controls. 嗨,我想调用C#方法。我已经尝试过webmethod,但是在c#方法中,我将无法访问文本框或其他控件。

this is how i done without success.. http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx 这是我没有成功的方法。.http ://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

With static works, but i want without static, so i can access to the controls in the aspx and validate the page. 使用静态作品,但我希望没有静态作品,因此我可以访问aspx中的控件并验证页面。

how can i do it? 我该怎么做?

You can't do this with just a javascript call, you need to postback if you want access to the controls on the page. 您不能仅通过JavaScript调用来执行此操作,如果要访问页面上的控件,则需要回发。 The page doesn't know what's in the textboxes for example unless you send that data, which a javascript call won't do, but a form or AJAX submit will. 例如,除非您发送该数据,否则该页面不知道文本框中的内容,而javascript调用则不会,但是表单或AJAX提交则可以。

I'd suggest a javascript validation library, something like this . 我建议使用javascript验证库, 类似这样的东西 Just be sure to also validate on the server once you've submitted to check for users who have javascript disabled (or are maliciously trying to bypass your validation...). 提交提交以检查禁用了JavaScript(或恶意尝试绕过您的验证...)的用户后,请确保在服务器上也进行验证。

You need to look at how to use an UpdatePanel , or a straight ajax callback . 您需要查看如何使用UpdatePanel或直接的ajax回调

With the UpdatePanel, you go through most of the page lifecycle, so you have access to the textboxes etc. 使用UpdatePanel,您将经历页面的大部分生命周期,因此可以访问文本框等。

Using an UpdatePanel may be the simplest path to what you're trying to accomplish. 使用UpdatePanel可能是您要完成的最简单的方法。

<asp:ScriptManager runat="server">
<asp:UpdatePanel runat="server">
  <ContentTemplate>

    ... your controls ...

  </ContentTemplate>
</asp:UpdatePanel>

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

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