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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Imran Javed ZiaConsultant 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_webguruCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.