Call a c# method from Javascript

☆樱花仙子☆ 提交于 2020-01-10 06:06:30

问题


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.

this is how i done without success.. 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.

how can i do it?


回答1:


You can't do this with just a javascript call, you need to postback if you want access to the controls on the page. 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.

I'd suggest a javascript validation library, something like this. 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...).




回答2:


You need to look at how to use an UpdatePanel, or a straight ajax callback.

With the UpdatePanel, you go through most of the page lifecycle, so you have access to the textboxes etc.




回答3:


Using an UpdatePanel may be the simplest path to what you're trying to accomplish.

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

    ... your controls ...

  </ContentTemplate>
</asp:UpdatePanel>


来源:https://stackoverflow.com/questions/2522967/call-a-c-sharp-method-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!