Pages

Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Calling Server side Event handler function Uisng Ajax Update pannel

1)Add Script manger to a page 2)Add Update panel in your page 3)Add any Server side standard Control . 4)In update panel call trigger tag .Inside trigger tag call asp:AysncPostBackTrigger control tag 5)Provide control id & eventname in above tag 6)Example below is assign Textbox value on buttin click
pseduocode aspx code //Asume one button control id="bttnAjax" and one textbox in content template of update pannel id=txtAjax"
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<Triggers >
<asp:AsyncPostBackTrigger ControlID="bttnAjax" EventName="click"/>
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtAjax" runat ="server"/>
</ContentTemplate>
</asp:UpdatePanel>

Code Behind
protected void bttnAjax_Click(object sender, EventArgs e)

{

txtAjax.Text="Welcome To Ajax World "

}







Calling code behind function using Ajax

1)Drag scriptmanger in your web page add attribute EnablePageMethods="true" 2)Create functions in javascript section (method name are similar example)       I)CallPageMethod()      II)CallParmeterPageMethod()      III)onSucceeded(result,userContext,MethodName)      IV))OnFailed(error,user) 3)Create static and return string function in Code behind (method name are similar example)     I)MyFirstPageMethod() add attribute [System.Web.Service.WebMethod()]     II)MyFirstParameterPageMehtod() add attribute[System.web.Service.WebMethod()] 4)Call javascript function on input button callPagemethod use event onclick
script code
function  CallPageMethod()
{
debugger;
PageMethods.MyFirstPageMethod(onSucceeded,onFailed);

}
function  CallParametersPageMethod()
{
debugger;
PageMethods.MyFirstParameterPageMethod("This is a Demo",onSucceeded,onFailed);

}

function onSucceeded(result,userContext,methodName)
{
function CallPageMethod()
{;
$get('div1').innerText=result;
}
function onFailed(error,user)
{
debugger;
alert("An error occurred");
}
code behind code [System.Web.Services.WebMethod()] public static string MyFirstPageMethod() { return "Welcome to Ajaax"; }       [System.Web.Services.WebMethod()] public static string MyFirstParameterPageMethod(string sVal) { return "Welcome To ajax world value passed is" + sVal ; }