Showing posts with label below. Show all posts
Showing posts with label below. Show all posts

Monday, March 26, 2012

Call Server Side method from JavaScript function in Master Page

I have a JavaScript function in a Master Page. The function is below.

function Add(q, p) {//call server side method }

I want to be able from within the JavaScript Add function defined above, call a C# server side method and pass the values q and p to it. No value is returned.

I have tried PageMethods.CallServerSideMethod(q, p); but it will only work in a Page, not a master page.

Is there any way I can do this in a master page?

How about using a web service call?

Thanks for your help.

How could i code that? An example maybe?


The documentation has pretty good code examples. See if this helps: http://www.asp.net/AJAX/Documentation/Live/tutorials/ExposingWebServicesToAJAXTutorial.aspx

Niall20:

it will only work in a Page, not a master page.

That's because .master file can't work as a httpHandler to server the request. It's blocked. In a simple word, the method in master page can't be accessed from client side directly.

Saturday, March 24, 2012

Call ASP.NET web service and return JSON using only html/javascript

I've implmented a pretty basic webservice as per below. I did a quick test in ASP.NET AJAX using the ScriptManager and it returned JSON-formatted data perfectly. I am now trying to do the same using just html/javascript with the XmlHttpRequest object.

Currently I've assigned the context-type to the XmlHttpRequest as is apparently required, and the webservice is "working", but only returning xml.Surprise

I'd really appreciate any help - or maybe even a better way to access the ASP.NET web service externally (i.e. outside of ASP.NET). Thanks.

PART A: ASP.NET Webservice snippet

[WebService(Namespace = "http://microsoft.com/webservices/";
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ScriptingService : OrYxBaseWebService {
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public ScriptPersonRoleList GetActiveCategoryManagers() {
OrYxAppStatus myStatus = BFManager.GetInstance().CreateStatus(GetDefaultRequest());
return BFManager.ScriptingBF.GetActiveCategoryManagers(ref myStatus);
}
}

PART B: Index html

<html>
<head>
<title>Ajax Test Drive</title>
<script type="text/javascript" language="javascript">

function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
// http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("not good at all..");
}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}

http_request.onreadystatechange = confirmResponse;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-Type", "application/json");

http_request.send(parameters);
}

function confirmResponse() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
document.getElementById('databox').innerHTML = result;
alert('Response:\n\n' + result);
} else {
alert('There was a problem with the request.');
}
}
}

function getData() {
var url = 'http://localhost/TVScriptingWebService/TVScripting.asmx/GetActiveCategoryManagers';
var request = makePOSTRequest(url, "");
}
</script>

</head>
<body onload="getData()">
<div style="width: 600px; padding: 10px; border: 1px solid;" align="center">
Test page</div>
<br>
<div id="databox">
</div>
<br>
</body>
</html>

What format do you want data in now?


I am wanting JSON not XML.


use text/json rather than application/json


Thanks for the response, I tried adding the following lines, but they all still return only xml as the responseText

http_request.setRequestHeader('Content-Type','text/json');


Could i again confirm that the webservice is actually returning JSON data?


The only time I have seen the webservice returns JSON data is when its called from within ASP.NET AJAX pages using the ScriptManager - which I don't want to do because these services are ultimately going to be called from a php application..

I am trying to call it using using only javascript/html and cannot get it to return JSON - only xml

Environment: ASP.NET 2.0, AJAX ASP.NET RC1. Tested on IIS5.1 and IIS6

Thanks


Well again it could be an inadvertant policy issue where microsoft doesnt want people to use other products just like datatable which is not returned as webservice. But you can always convert xml to json.

Let me see your code for consuming page in .net


Hi naturehermit,

Thanks again. You're right I could parse the xml into Json within the client, however I think that would eliminate one of the main advantages of using JSON..Wink

I couldn't get it to work so I've implemented my own IHttpHandlerFactor and IHttpHandler classes which was pretty easy. They now intercept the webservice calls and check whether use my classes, which return JSON using the JavaScriptSerializer, or the generic WebServiceHandlerFactory().

Currently the flag I've decided on is if there is an "application/OrYxJson" within the Context-Type property of the request. If this property is set, JSON gets returned, otherwise you get good old SOAP.

Early days, but so far so good..


Good luck mate, let me know if i could be of any help. Its always good to share your experiences


Yep agreed and willdo. Thanks.


naturehermit:

What format do you want data in now?


?


Hi,

I ran into same issue where I specified in the webservice JSON and yet XML was returned. Anyway, I found ot that it actually returns JSON. Try running fiddler to intercept the response from the webservice and you'll see the JSON string. Ignore the "_type".

Thanks

Wednesday, March 21, 2012

CalendarExtender question

How do I get the CalendarExtender to close when a date is selected. I tried below, but when I click a date, the textbox is populated, but the calendar is still displayed.

<asp:TextBoxID="TextboxEnd"runat="server"Text="enter end date"/>

<ajaxToolkit:CalendarExtenderID="CalendarExtenderEnd"runat="server"TargetControlID="TextBoxEnd"Animated=true/>

That's funky...we had the opposite problem: if you brought up the calendar but didn't want to select a date...just make it disappear...and clicked anywhere else on the screen, it wouldn't hide the calander until you selected a date.

We found that the new version of the Toolbox that was released recently fixes our issue...it might fix yours too...are you using the updated Toolbox controls or the older ones?

Just curious: does this happen in Firefox and IE, or just in one?


It happens in both IE and FF. I think I am using the new ones. I downloaded about a month ago. Was there a new build since then? The only way I can get the calendar to disappear is to click somewhere else on the screen.


Thanks, D'Arcy. I downloaded the September 2007 release and it's working great now.

CalendarExtender positioning

When binding the CalendarExtender to an target textbox, the calendar will display immediatly below the textbox.
Is there a way to control the positioning of the calender so it will appear for example abowe the textbox?

Hi,

you can use

PopupPosition="TopRight" / TopLeft / BottomLeft/ BottomRight

in CalendarExtender tag property

hope it helps


Thanks, this answered my question.
Your answer made me realize that didn't have the latest release of the toolbox installed.
The one I had didn't support the PopupPosition attribute.Thanks

CalendarExtender Pops up below DropDownList - need help

Hi, I've used CalendarExtender with a TextBox. In the next row I had a dropdownList. Now, when I click the imagebutton, the Calender pops-up below the DropDownList.

Can any one help me with this?

Hi,

Please refer to this post: http://forums.asp.net/p/834115/834115.aspx#834115