本文介绍了PageMethods和的UpdatePanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面层次结构如下所示

I have a page hierarchy as the following

我要执行PageMethod的,如果我点击保存按钮,所以我codeD像下面的

I want to execute a PageMethod if I click the 'SAVE' button, so I coded like the following

OnClientClick="return btnSaveAS_Clicked()"

被叫在内的用户控制的pageLoad的以下

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页方法

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

但它提供了以下错误...

But it gives the following error...

推荐答案

您不能定义在ASCX页页的方法。你在你的Web表单来定义它们。如果你想有一个页面的方法,在用户控件定义,你必须在你定义的转发页面的方法的aspx页面如下图所示(来源):

You cannot define page methods in ascx pages. You have to define them in your web form. If you want to have a page method, defined in your user control, you'd have to define a forwarding page method in you aspx page like below (source):

在用户控件:

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

在aspx.cs页:

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

和在aspx页面:

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

另外,你可以使用ASMX服务和的jQuery AJAX方法(的,,的)异步调用你的方法(sample).

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

另一个选择是定义HTTP处理程序,并通过jQuery调用它们,以及(的)。

Another option would be defining http handlers and call them via jQuery as well (tutorial).

这篇关于PageMethods和的UpdatePanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:46