jquery .net not firing the function $.ajax - c#

im using jquery in asp.net, if i try to use $.ajax functionality, i got this
if i use it in a separate page it works..
when i put it in an ascx and put the ascx out of <form runat="server" >... tags it works
if i put it between <form> tags , jquery works but it doesnt fire $.ajax event

In my experience most jQuery code should go in $(document).ready(),
this is so that the DOM has loaded and the content is there,
have you tried that?
There is some good info on that here.
If that dosen't work, maybe post some code you are using?
HTH

ive solved it. the problem was with asp .net master pages, there are many ways of sending a post-get request from jquery in ajax, but it seems only some of them work in asp .net, ive posted the code for a chat control in c# in http://code.google.com/p/micachat/
an example that works for get request
$.ajax({
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Content-Type", "application/json");
},
type: "GET",
url: "./chatControl/processmessage.aspx?idportal=<%=Request["idportal"] %>",
data: "message=" + $('#message').val() + "&name=" + $('#name').val() + "",
dataType: "text",
success: function(msg){ $("#myDiv").text( "Data Received: " + msg ); }
}); // end of ajax

Related

How do you sync the code behind when updating a user control using javascript?

I have a custom user control that serves as a reusable ASP DropDownList that asynchronously retrieves data from an RESTful endpoint
<myControls:AjaxDropDown ID="ddlDropdown" runat="server" ServicePath="/MyWebService2.asmx/GetLetterTypes"/>
Inside the user control, I'm updating the drop down content via success in $.ajax
$.ajax({
type: "GET",
dataType: "json",
url: $('#<%=textBoxUrlPath.ClientID %>').val(),
contentType: "application/json; charset=utf-8",
async: true,
success: function(res) {
onGetLetterTypesSuccess(res);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
How do I sync the data retrieved by $.ajax with the code behind for the user control?
The reason I'm using $.ajax rather than doing a partial update via the UpdatePanel control is due UpdatePanel's inability to have multiple asynchronous calls at a time on a single page.
I decided to not go this route of client side updating, since it's directly opposing the Web Form structure. Instead I'll settle for a synchronous solution of make this call from the code behind and populating the DropDownList before serving the Page.
I guess you can use asp.net hidden fields to store the selected values but yes it could become unnecessarily cumbersome through client side.

How to post back the html page source of the current page that is loaded(no refresh)?

The subject says it all but i'll further explain. If i am on a page that has a button on it, how can i wire up the button to capture the page source and send it back to the controller for storing it? I can store it as byte array or any file type. I can't find any solutions for posting back the page source of the current page. All the examples i have found use something like WebClient which you supply a url for it to load up and then capture the html.
if you have a button with id="btn" here is the sample:
<script>
$('#btn').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
data: $('html').html(),
url: 'exmaplesite.com/postaction',
success: function () {
alert('success');
}
});
})
</script>

Writing to SQL Server Compact database from jquery mouseup event

I'm working on an asp.net c# empty web forms project. I have a SQL Server Compact database with a single table that I want to use to store information about an elements (x,y) coordinates.
The database should be updated with the new coordinates for a particular panel when the mouseup event is triggered on that panel (a div essentially). The solution doesn't need to be ajax, submitting the page will be ok.
The problem is that there is no mouseup type of event for an <asp:Panel> element and on the other side I'm not sure how I would trigger something server-side from jQuery. Getting the coordinates using jQuery is easy enough, but what would I do from there?
Are hidden inputs ok for this? Give me your best solution - I don't need to see code, just give me a better idea of what i should be doing.
Thanks!
First check what exactly asp:Panel is getting rendered in browser by firebug, suppose it renders as div then you can use jquery's .mouseup() function and bind this event to what ever element that panel is rendered. Suppose if that asp:panel is rendered as div then look this below code for sample.
<div id="asp_panel">
Click here
</div>
$('#asp_panel').mouseup(function() {
$.ajax({
url: "HelloWorld.asmx/Hello",
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}", // if your method takes any parameter pass it in here in json format.
dataType: "JSON"
success: function(msg) {
alert(msg);
}
});
});

JQuery and parameters

I am creating Accordion menu using JQuery. I want to hide links according to the user status. How to pass values from code behind to jquery during pageload?
Eg:
UserA:
Menu header: headerA and HeaderB
UserB:
Menu header: headerB and HeaderC
I want to pass header names to jquery to hide it.
Geetha
I assume that by 'user status' you mean logged in/logged out. So, If you are using a server side templating languages, such as JSP/JSTL, you can easily do a <c:choose /> and add the links in your HTML page. Like,
<c:choose>
<c:when test='${user.loggedin == "yes"}'>
Hello, <c:out value='${user.name}' /> Log out
</c:when>
<c:otherwise>
Log in
</c:otherwise>
</c:choose>
It's best to print style="display:none;" in tags you want to hide on the server-side because otherwise they will be visible to the user while the page is loading and then disappear when the javascript is executed (especially in slow browsers). This way the link will be hidden while the page is loading as well.
E.g:
Hidden Link
I got solution by using the following code.
$.ajax({
type: "POST",
url: "NavigationMenu.aspx/UserStatus",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});

Closing an aspx page after redirect to a clickonce application

I have code in code behind portion of my aspx page. On a button click a function gets called so that a clickonce application loads. Originally I was doing all of this in javascript. The js set the window.location to the url of my clickonce application, and close through a timeout. This worked fine until I installed the application on another server. IE does not allow the clickonce application to get loaded through client side script. I am now forced to do a redirect to the url of the clickonce application. The problem that I'm encountering now is not having access to be able to close the window where the redirect was initiated from. The redirect fires first before any js could run. I basically need a way to slow down the redirect so that i can run my js.
You could redirect to a page that will have the JavaScript you had before - to close the window and redirect to the clickonce application. You could pass the URL of the application to this page in the query string. The page could be plain html.
you could do something like this. I am using jquery but you dont have too..
webmethod = GetUrlToApplication, is basically returning path.
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetUrlToApplication",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: ajaxSucceededFn,
error: errorFn
});
});
function errorFn ()
{
alert('Error:-Unable to launch Application.');
}
function ajaxSucceededFn (result)
{
window.location = result;
setTimeout(function() { window.open('', '_self', ''); window.close(); }, 1000);
}
</script>

Categories