RadAjax and Code Behind
Hello,
I have a custom JQuery plugin on my page (in a user control) that makes an AJAX call to get some JSON data. As I'm new to .NET AJAX development and Sitefinity + RadControls, I'm wondering how I can setup that my JQuery plugin makes the AJAX request to call a function of the code behind of my custom widget/user control and that method returns a JSON string back to the plugin. What does it take to do this?
Thanks, Andy
Hi Andy,
You could try to make a call to a webservice. You have to expose the methods in the code-behind.
Either make a WebAPI class with the functions you need, or try a WCF Service. Here's an example of such a service:
namespace
SitefinityWebApp.Sitefinity.Services.Survey
[ServiceBehavior(IncludeExceptionDetailInFaults =
true
)]
[ServiceContract(Namespace =
""
)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public
class
SurveySrv
[OperationContract]
[WebInvoke(Method=
"POST"
)]
public
ScoreResponse Score(
string
[] answerIds)
ScoreResponse result =
new
ScoreResponse() Score = -1 ;
if
(answerIds ==
null
|| answerIds.Length == 0)
return
result;
//convert the strings to GUID
List<Guid> userAnswers =
new
List<Guid>(answerIds.Length);
for
(
int
i = 0; i < answerIds.Length; i++)
userAnswers.Add(Guid.Parse(answerIds[i]));
//calculate the score
BusinessLogic businessLogic =
new
BusinessLogic(Config.Get<DataConfig>().ConnectionStrings[
"Sitefinity"
].ConnectionString);
Score score = businessLogic.CalculateScore(userAnswers,
"pers"
+ Guid.NewGuid(),
true
);
result.Score = score.Value;
return
result;
Then in your script code you could do something like this:
//makes an ajax call in order to get the score
//callback - must be a function; if it is specified as parameter, it will be called after the ajax call returns with success
function
surAjaxGetScore(callback)
//build the array with the question ids
var
selectedAnswers = surGetSelectedAnswers();
var
result = $(
'.done-box span.speed'
);
//send the array to the server
$.ajax(
url: baseUrl +
"/Sitefinity/Services/Survey/SurveySrv.svc/Score"
,
data: JSON.stringify( answerIds: selectedAnswers ),
type:
"POST"
,
contentType:
"application/json; charset=utf-8"
,
dataType:
"json"
,
success:
function
(msg)
var
objScore = eval(msg);
result.text(objScore.d.Score);
if
(callback)
callback();
);
I initialize this code from a Usercontrol. Hope this helps?
Regards,
Daniel
Thanks for the info, Daniel. That does make sense, and I will probably try to use one of the Sitefinity built-in web services to get my information as that should work fine.
Out of curiosity, I'm still wondering if there is a way to call code behind via AJAX and not through a web service? It's more of a theoretical question as I'm normally a Java/Struts2 developer that is trying to transfer knowledge to the .NET world. I've read about the PageMethods decorator, but that doesn't apply for user controls.
Thanks, Andy
Hi Andy,
I'm not sure, never tried it before. I think it is not possible. You could use the RadAjaxManager or RadAajaxPanel to 'ajaxify' your calls from code-behind.
Now when I type the above, I think it would be possible, right? ;)
Regards,
Daniel
That does make sense! I've never had any experience with those controls and have no idea how they work. But it sounds like the more standard way is to use the web services anyway.
Yeah, just develop the UserControl as you would, with postback and all. Then put a RadAjaxManager on the UserControl and by using the designer you can set which control(s) should cause an ajax-action and set which control(s) will be updated by that action.
Should be not too complicated?
Regards,
Daniel
When you say to set which controls will be updated and which controls will cause the action, what if they're one in the same? i.e. my plugin is an event calendar that loads the events on page load via AJAX and updates itself with the new information.
Thanks, Andy
Updating itself will also work, no problem.
Regards,
Daniel
The theory certainly makes sense. Do you know of any samples that incorporate this with JQuery as opposed to other RadControls? I don't need any of the RadControls with the exception of the RadAjaxManager, but it seems that it is constantly looking for "ControlID"s that seem to reference other RadControls?