Call ASP.NET function from JavaScript?

后端 未结 20 1932
暗喜
暗喜 2020-11-22 07:21

I\'m writing a web page in ASP.NET. I have some JavaScript code, and I have a submit button with a click event.

Is it possible to call a method I created in ASP with

相关标签:
20条回答
  • 2020-11-22 08:10

    Add this line to page load if you are getting object expected error.

    ClientScript.GetPostBackEventReference(this, "");
    
    0 讨论(0)
  • 2020-11-22 08:11

    Please try this:

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

    ddlVoucherType is a control which the selected index change will call... And you can put any function on the selected index change of this control.

    0 讨论(0)
  • Well, if you don't want to do it using Ajax or any other way and just want a normal ASP.NET postback to happen, here is how you do it (without using any other libraries):

    It is a little tricky though... :)

    i. In your code file (assuming you are using C# and .NET 2.0 or later) add the following Interface to your Page class to make it look like

    public partial class Default : System.Web.UI.Page, IPostBackEventHandler{}
    

    ii. This should add (using Tab-Tab) this function to your code file:

    public void RaisePostBackEvent(string eventArgument) { }
    

    iii. In your onclick event in JavaScript, write the following code:

    var pageId = '<%=  Page.ClientID %>';
    __doPostBack(pageId, argumentString);
    

    This will call the 'RaisePostBackEvent' method in your code file with the 'eventArgument' as the 'argumentString' you passed from the JavaScript. Now, you can call any other event you like.

    P.S: That is 'underscore-underscore-doPostBack' ... And, there should be no space in that sequence... Somehow the WMD does not allow me to write to underscores followed by a character!

    0 讨论(0)
  • 2020-11-22 08:12

    The Microsoft AJAX library will accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions.

    I suggest the Microsoft AJAX library. Once installed and referenced, you just add a line in your page load or init:

    Ajax.Utility.RegisterTypeForAjax(GetType(YOURPAGECLASSNAME))
    

    Then you can do things like:

    <Ajax.AjaxMethod()> _
    Public Function Get5() AS Integer
        Return 5
    End Function
    

    Then, you can call it on your page as:

    PageClassName.Get5(javascriptCallbackFunction);
    

    The last parameter of your function call must be the javascript callback function that will be executed when the AJAX request is returned.

    0 讨论(0)
  • 2020-11-22 08:12

    It is so easy for both scenarios (that is, synchronous/asynchronous) if you want to trigger a server-side event handler, for example, Button's click event.

    For triggering an event handler of a control: If you added a ScriptManager on your page already then skip step 1.

    1. Add the following in your page client script section

      //<![CDATA[
      var theForm = document.forms['form1'];
      if (!theForm) {
          theForm = document.form1;
      }
      function __doPostBack(eventTarget, eventArgument) {
          if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
              theForm.__EVENTTARGET.value = eventTarget;
              theForm.__EVENTARGUMENT.value = eventArgument;
              theForm.submit();
          }
      }
      //]]>
      
      1. Write you server side event handler for your control

        protected void btnSayHello_Click(object sender, EventArgs e) { Label1.Text = "Hello World..."; }

      2. Add a client function to call the server side event handler

        function SayHello() { __doPostBack("btnSayHello", ""); }

    Replace the "btnSayHello" in code above with your control's client id.

    By doing so, if your control is inside an update panel, the page will not refresh. That is so easy.

    One other thing to say is that: Be careful with client id, because it depends on you ID-generation policy defined with the ClientIDMode property.

    0 讨论(0)
  • 2020-11-22 08:15

    Try this:

    if(!ClientScript.IsStartupScriptRegistered("window"))
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "window", "pop();", true);
    }
    

    Or this

    Response.Write("<script>alert('Hello World');</script>");
    

    Use the OnClientClick property of the button to call JavaScript functions...

    0 讨论(0)
提交回复
热议问题