Reputation: 10755
I am attempting to read the session value from an asp.net application in jQuery. I know that this requires either a hidden field, a page method of jQuery ajax. I chose to go with the hidden element, which is simple enough:
<asp:HiddenField runat="server" ID="SessionAdmin" />
Then in the code behind I populate it like so
SessionAdmin.Value = Session("selectedcompanypriviledge").ToString().ToLower()
I then attempt to use it like so:
var isAdmin = $("#SessionAdmin").val();
alert($("#SessionAdmin").val());
if (isAdmin != "coadm" && isAdmin != "tcadm" && isAdmin!= "taagt") {
$("#htl_gds").hide();
$("#htl_propcode").hide();
$("#htl_RFPId").hide();
}
else {
//alert(isAdmin);
$("#htl_gds").show();
$("#htl_propcode").show();
$("#htl_RFPId").show();
}
' it works the first time wonderfully, the alert gives me what I'm expecting and the items are shown. It's when I click the next link (or any link after that) then I get 'undefined for the value of isAdmin. Any idea why this is happening?
Upvotes: 0
Views: 67
Reputation: 107536
When accessing ASP.NET server side controls by ID you need to remember that ASP.NET mangles the actual ID rendered on the client (by prepending its parent naming container names to it. It's better to find the actual ID of the field by using a little inline script:
$("#<%= SessionAdmin.ClientID %>").val();
Now that you're accessing the value from the field correctly, any other issues are probably related to how that field's value is set in the code-behind. Make sure that code is running on every post-back in the Page_Load
method (for example). Also make sure that you're running your JavaScript code only after the DOM is ready:
$(function() {
// Your existing JavaScript code here
});
Upvotes: 1