to execute the code written in code behind (c#, vb.net). we need to know that the exists on server, not at client. so it is needed to make a postback to reach the code.
to avoid full postback we can use AJAX.
put the code below in your .aspx.cs file
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebForm1));
DropDownList1.Attributes.Add("onchange","CallBack();");
}
[Ajax.AjaxMethod()]
public int ServerSideAdd(int firstNumber,int secondNumber)
{
return firstNumber + secondNumber;
}
Put the given below code in your aspx file
< script language="javascript" >
function CallBack()
{
val1=document.getElementById('txtValue1').value;
val2=document.getElementById('txtValue2').value;
WebForm1.ServerSideAdd(val1,val2, ServerSideAdd_CallBack);
}
function ServerSideAdd_CallBack(response)
{
alert(response.value);
}
< /script >
< body MS_POSITIONING="GridLayout" >
< form id="Form1" method="post" runat="server" >
< INPUT onclick="CallBack()" type="button" value="Button" runat="server" >
< asp:dropdownlist id="DropDownList1" style="Z-INDEX: 101; LEFT: 224px; POSITION: absolute; TOP: 48px"
runat="server" >
< asp:ListItem Value="Rajesh" >Amit< /asp:ListItem >
< asp:ListItem Value="Rolen" >Ajay< /asp:ListItem >
< asp:ListItem Value="JSJodha" >Arun< /asp:ListItem >
< asp:ListItem Value="Mukesh" >Ravi< /asp:ListItem >
< /asp:dropdownlist >< asp:button id="Button1" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 160px" runat="server"
Text="Button" >< /asp:button >
< asp:TextBox id="txtValue" style="Z-INDEX: 103; LEFT: 304px; POSITION: absolute; TOP: 88px" runat="server" >< /asp:TextBox >
< asp:TextBox id="txtValue1" style="Z-INDEX: 104; LEFT: 312px; POSITION: absolute; TOP: 232px"
runat="server" >< /asp:TextBox >
< asp:TextBox id="txtValue2" style="Z-INDEX: 105; LEFT: 520px; POSITION: absolute; TOP: 200px"
runat="server" >< /asp:TextBox >< /form >
< /body >
Call codebehind server code from javascript
Posted by
Rajesh Rolen
at
Monday, July 27, 2009
Labels: ASP.NET , java script
0 comments:
Post a Comment