Storing Hidden Asp field value

I have javascript to test screen size and choose style sheet and defined a string variable in javascript.  I have the variable stored in a hidden field value  Now I want to store the value into a session variable to used throught program.
<script type="text/javascript" language="javascript">
        
         $(document).ready(function () {
           
             if (screen.width < 480) {
                 $('.basicStyles0').attr('href', "css/main.css");
                 var a = "desktop";
                 $("#<%= hfTest.ClientID %>").val(a);
             }

             else {
                 $('.basicStyles0').attr('href', "css/mobile_main.css");
                 var b = "mobile";
                 $("#<%= hfTest.ClientID %>").val(b);


             }
         });
    
    
    </script>

Open in new window

<asp:HiddenField ID="hfTest" runat="server" />

Open in new window

zrazakAsked:
Who is Participating?
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Us the following Code

Change javascript as
$(document).ready(function () {  
             
             if (screen.width < 480) {  
                 $('.basicStyles0').attr('href', "css/main.css");  
                 var a = "desktop";  
                 $("#<%= hfTest.ClientID %>").val(a);  
             }  
  
             else {  
                 $('.basicStyles0').attr('href', "css/mobile_main.css");  
                 var b = "mobile";  
                 $("#<%= hfTest.ClientID %>").val(b);  
             }  
             __doPostBack('ScreenValue', '');
         });

Open in new window


and
 
In code behind use in Page load event 

if (this.IsPostBack)
{
 if (this.Request.Form["__EVENTTARGET"].Equals("ScreenValue", StringComparison.CurrentCultureIgnoreCase))
 {
    Session["YourSessionKey"] = hfTest.Value;
}
}

Open in new window


Thanks
0
 
zrazakAuthor Commented:

IJZ code looks great I get an error that is undefined
 __doPostBack('ScreenValue', '');
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:
can you tell me the ASP.NET version?
0
 
zrazakAuthor Commented:
Version 4.0
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Place a dummy asp:button on the page and check if it does post back or not. as it also use the sam efunction internally.



0
 
zrazakAuthor Commented:
I am not sure what to do.  Doesn't work
0
 
zrazakAuthor Commented:
Leakim can you help me implement the ajax call to web
0
 
khan_webguruConnect With a Mentor Commented:
Creating a page method for testing purposes.

Writing a page method is easy. They must be declared as static, and they must be decorated with the [WebMethod] attribute. Beyond that, ASP.NET AJAX takes care of the rest on the server side.
This will be our page method for the example:

 
public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

Open in new window



Calling the page method with jQuery instead

Knowing that a page method is consumed in the same way as a web service, consuming it with jQuery isn’t difficult. For more detailed information, see my previous article about making jQuery work with ASP.NET AJAX’s JSON serialized web services.

Using the jQuery.ajax method, this is all there is to it:

 
$.ajax({
  type: "POST",
  url: "PageName.aspx/MethodName",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

Open in new window


Putting it all together.
Corresponding to the example page method above, here’s our Default.aspx:

 Snippet ID=8087149

As you can see, there’s no ScriptManager required, much less EnablePageMethods.
As referenced in Default.aspx, this is Default.js:

 
$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});

Open in new window


The end result is that when our result div is clicked, jQuery makes an AJAX call to the GetDate page method and replaces the div’s text with its result.
If you’re wondering about the msg.d, be sure to see my post about what it is, why it’s there, and why you may not need it in your version of ASP.NET: A breaking change between versions of ASP.NET AJAX

hope this will help you to implement and understand AJAX call.

Regards,

Asif Ahmed Khan
<html>
<head>
  <title>Calling a page method with jQuery</title>
  <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
  <script type="text/javascript" src="Default.js"></script>
</head>
<body>
  <div id="Result">Click here for the time.</div>
</body>
</html>

Open in new window

0
 
khan_webguruCommented:
Hope my last post will give you the IDEA about AJAX
0
 
zrazakAuthor Commented:
It gave me an idea
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.