JQuery and parameters - c#

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
});

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.

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);
}
});
});

.Net - call client side and server side method on click of asp:LinkButton

I have an asp:LinkButton which require following -
- call jQuery to open link url in another tab of current browser
- then call server side method to perform some logging stuff
Please guide me for a
safe (which can help avoid having 'popup blocked' messages as far as possible), I understand I shouldn't override any user personal setting for popup blocker but as it is a direct user click on asp:LinkButton, was wondering if there is a neat solution to avoid Popup blocker messages and
an efficient and user friendly way of calling server side method (where I can avoid postback)
Thank you!
Having Javascript make a call like:
window.open("http://www.google.com");
will always run the risk of being caught by a popup blocker. Is there a reason why you can't just set target="_blank" on the <a> tag?
As far as calling a server-side method without doing a postback, it's not possible. You could use JQuery's ajax function to make a call to a server side method where the page doesn't refresh, but a post-back must still occur, whether it is visible to the user or not.
Here is what that looks like:
$.ajax({
type: 'POST',
url: "http://www.yourdomain.com/pageToPostTo.aspx",
data: "your data here",
success: function(data) {
alert("It worked");
},
dataType: "json"
});

jquery .net not firing the function $.ajax

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

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