PageMethods and UpdatePanel

I have a page hierarchy as shown below.

enter image description here

I want to execute the PageMethod method if I click the "SAVE" button, so I am encoded as follows

On Button Click I called

OnClientClick="return btnSaveAS_Clicked()" 

The following is called up on the Load page of the internal user control

 private void RegisterJavaScript() { StringBuilder jScript = new StringBuilder(); jScript.Append("<script type='text/javascript'>"); jScript.Append(@"function btnSaveAS_Clicked() { var txtConditionName = document.getElementById('" + txtConditionName.ClientID + @"').value; PageMethods.Combine('hello','world', OnSuccess); function onSuccess(result) { alert(result); } }"); jScript.Append("</script>"); Page.ClientScript.RegisterStartupScript(this.GetType(), "conditions_key", jScript.ToString()); } 

Coded page method as

 [WebMethod] public static string Combine(string s1, string s2) { return s1 + "," + s2; } 

But this gives the following error ...

enter image description here

+6
source share
1 answer

You cannot define page methods on ascx pages. You must define them in your web form. If you want the page method defined in the user control, you need to determine the method of the forwarding page on the aspx page, as shown below ( source ):

in user management:

 [WebMethod] [ScriptMethod(UseHttpGet = true)] public static string MyUserControlPageMethod() { return "Hello from MyUserControlPageMethod"; } 

on aspx.cs page:

 [WebMethod] [ScriptMethod] public static string ForwardingToUserControlMethod() { return WebUserControl.MyUserControlMethod(); } 

and on the aspx page:

  function CallUserControlPageMethod() { PageMethods.ForwardingToUserControlPageMethod(callbackFunction); } 

Alternatively, you can use ASMX services and jquery ajax methods ( jQuery.ajax , jQuery.get , jQuery.post ) to asynchronously call your methods ( sample ).

Another option would be to define HTTP handlers and call them through jQuery ( tutorial ).

+4
source

Source: https://habr.com/ru/post/893551/


All Articles