Page method is a static special method in ASP.NET page code behind. Using page method is an easy way to reduce post backs in your web page.
In order to use Page method, you need to follow these steps:
1. In your ASP.NET web page, add the following line inside form tag
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
2. Inside your ASP.NET web page code behind, define a public static method as page method:
3. In your aspx file, add the javascript event handler for the control's client onclick or onchange events.
Page method accepts two extra parameters besides the one defined in your method parameters. The first extra parameter is the function that will be called when the page method invokes successfully. The second extra parameter is the function that will be called when the page method fails.
In order to use Page method, you need to follow these steps:
1. In your ASP.NET web page, add the following line inside form tag
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
2. Inside your ASP.NET web page code behind, define a public static method as page method:
[WebMethod()]
public static string[] GetList(string[] arrList)
{
string[] arrNewList = new string[arrList.Length + 1];
for (int i = 0; i < arrNewList.Length; i++)
{
arrNewList[i] = "Item " + i;
}
return arrNewList;
}
3. In your aspx file, add the javascript event handler for the control's client onclick or onchange events.
<script type="text/javascript" language="javascript">
var ddlControl;
function InvokePageMethod(ddlControlId)
{
ddlControl = document.getElementById(ddlControlId);
var arrList = new Array();
for(var i=0; i < ddlControl.options.length; i++)
{
arrList[i] = ddlControl.options[i].text;
}
PageMethods.GetList(arrList, OnActivitySucceed);
return false;
}
function OnActivitySucceed(result)
{
ddlControl.options.length = 0;
for(var i=0; i < result.length; i++)
{
var option = document.createElement('option');
option.value = i;
option.text = result[i];
ddlControl.options.add(option);
}
}
</script>
Comments