Tuesday, February 17, 2009

ajax json jquery call with passing parameters using ASP.Net

If you want to load a content faster than before, you need to use ajax to load content. To get started you need to:

HTML

<div id="EventContent" class="loading"/>

SCRIPT


$.ajax({
type: "POST",
url: "RSSReader.asmx/GetRSSReader",
data:
"{controlName:'events.ascx'}",
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function(data)
{
$('#EventContent').removeClass('loading').html(data);
}
});



APP_CODE

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public
class RSSReader : System.Web.Services.WebService {
[WebMethod]
public
string GetRSSReader(string controlName)
{
Page page = new
Page();
UserControl ctl =
(UserControl)page.LoadControl("~/controls/" +
controlName);
page.Controls.Add(ctl);
StringWriter writer = new
StringWriter();
HttpContext.Current.Server.Execute(page, writer,
false);
return writer.ToString();
}

}



Notice that the bold text is the parameter name and it have to be same. It is recommended to be string

4 comments:

Anonymous said...

I wish not agree on it. I assume polite post. Particularly the title-deed attracted me to review the intact story.

Anonymous said...

Genial post and this fill someone in on helped me alot in my college assignement. Thanks you seeking your information.

Anonymous said...

Hi
Very nice and intrestingss story.

Anonymous said...

Ι useԁ to be able to find good info from your
blοg poѕts.

My webpаge - Online Casino Bonus Spil i Danmark - Finding The very best Online Casino Perk