Call a c# method from Javascript

前端 未结 3 2085
面向向阳花
面向向阳花 2020-12-22 06:51

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

相关标签:
3条回答
  • 2020-12-22 07:26

    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...).

    0 讨论(0)
  • 2020-12-22 07:42

    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.

    0 讨论(0)
  • 2020-12-22 07:52

    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>
    
    0 讨论(0)
提交回复
热议问题