Calling Code-behind from Javascript

前端 未结 3 1512
借酒劲吻你
借酒劲吻你 2020-12-18 11:11

On the click of a button, I call a JavaScript function. After getting the value, I need to perform some stuff from the value obtained in the code-behind. How should I call c

相关标签:
3条回答
  • 2020-12-18 11:23

    You can put the server side code into a web service, make a service reference in an asp:ScriptManager on your aspx page and then you can call/execute the web service from javascript by calling:

    WebServiceClassName.MethodName(javascriptvariable, doSomethingOnSuccess)
    

    Here is a link on doing that:

    http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

    0 讨论(0)
  • 2020-12-18 11:30

    You can call the __doPostBack Event.

    function openWindow(page) {
      var getval = window.showModalDialog(page);
      document.getElementById("<%= TxtInput.ClientID %>").value = getval; 
    __doPostBack('btnSelectImage', getval);
    }
    

    And on the server side in your code behind, you can get the value:

    In the PageLoad method:

    if (Request.Form["__EVENTTARGET"] == "btnSelectImage")
    {
        //get the argument passed
        string parameter = Request["__EVENTARGUMENT"];
        //fire event
        btnSaveImage_Click(this, new EventArgs());
    }
    
    0 讨论(0)
  • 2020-12-18 11:44

    yes there is a way.

    first, you can use javascript to submit the form after your return value is set in TxtInput.

    function openWindow(page) {
      var getval = window.showModalDialog(page);
      document.getElementById("<%= TxtInput.ClientID %>").value = getval; 
      document.forms[0].submit();
    }
    

    then in your code behind, you can handle TxtInput's value in page load event.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (this.Input.Value != string.Empty)
            {
                this.Input.Value += "blah";
            }
        }
    }
    

    note: you may need Identifying control that caused postback

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