C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Javascript »

How to call a serverside method in ASP.NET using javascript


Posted Date: 11 Nov 2008    Resource Type: Code Snippets    Category: Javascript
Author: AbhiMember Level: Diamond    
Rating: 1 out of 5Points: 10



Sometime we need to do something on client side, no postback will be there for that we can call server side method from client side.

For example I need information for binding a drop down to bind another drop down.
on change of drop down I will call javascript function.


<asp:dropdown id=ddlDropdown runat=server/>

on page load
ddlDropdown.Attributes.Add("onchange","callMethod();")
<script langauge=javascript type="text/javascript">
function callMethod()
{

var pkval = Document.getElementbyId("ddlDrop").value;
PageMethods.FindData(pkVal,OnSucceeded,OnFailed);//finddata is serverside static method.
}
function OnSucceeded(result)
{
FillData(result);
}


function OnFailed(error, userContext, methodName)
{

alert("failure") ;
}
</script>

inserverside

add namespace
using System.Web.Services;


write code for that method

[WebMethod]//must be there or without adding using System.Web.Services; you directly add here
[System.Web.Services.WebMethod]
public static string RetriveData(string args)
{
int id = convert.Toint32(args);

//create a dataset use sql query like (where pkid = id)from another table

// remember the column will be your xml node so you can rename in your query like like pk_primary as pk
DataSet dsData= //add code as your requirement
//after creating dataset convert to xml
dsData.DataSetName = "CONTROLS";
XmlDataDocument xDoc = new XmlDataDocument(dsData);
string xml = xDoc.OuterXml;
xml = xDoc.InnerXml;
return xml;


}



result xml file does to here
function OnSucceeded(result)
{
FillData(result); // call another function
}

function FillData(xmlVal)
{
//find the node values
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(result);
var controls = doc.getElementsByTagName('Table');
for(var i = 0; i < controls.length ; i++)
{
var Contol_type = controls[i].getElementsByTagName('columnname as you assign in query in serverside')[0].childNodes[0].nodeValue;

//assign that Contol_type to a another control or dropdown
}
}


This is an example of how to call serverside method.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
WebMethod  .  Serverside method  .  Javascript  .  Asp.net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Number validation with digit grouping in Javascript
Previous Resource: Example on JavaScript
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use