Storing a value from javascript in asp.net

Is there any way I can store a value from java script code to asp.net control wheter mobile or desktop.  This will allow to store variable in code behind that I can further use.  Any help with this code is greatly appreciated.

The code that i have is.

I have code that calls a style sheet
 <link id="basicStyles" class="basicStyles0" rel="Stylesheet" href="css/main.css" type="text/css"/>
then I have javascript code to detect in mobile then call style sheet.
<script type="text/javascript" language="javascript">
         $(document).ready(function() {
       
            if (screen.width > 480) {
                $('.basicStyles0').attr('href', "css/main.css");
            }
            else {
                $('.basicStyles0').attr('href', "css/mobile_main.css");
               
            }
        });
   
   
    </script>




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.

drainCommented:
I don't think there is any clean way to do it. Here's some attempts I found on another forum:
http://forums.asp.net/p/1203356/2127889.aspx#2127889
Basically it looks like you'd have to do another postback, which maybe you could do through a control (rather than the whole page).

you can also detect browser type through .net which would be another way to approach the problem
http://msdn.microsoft.com/en-us/library/3yekbd5b.aspx

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
zrazakAuthor Commented:
I read that this can be implemented using a asp.net hidden field control and running asp.net code to insert a value into it?  Do you know how to implement?
drainCommented:
That's what's going on in the first link I submitted.
You have to do a postback to collect the value though. When the page first loads the javascript is fired after the asp.net code runs.  
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

khan_webguruCommented:
Hello Bro!

ASP.NET your required code. Hope that will help you.
At the end developed a custom web control which does a round trip to the server for the first time and then add a two session variables to the user's session which can be directly consumed by the other controllers.  
 
this.lblHeight.Text = Session["ScreenHeight"].ToString();
this.lblWidth.Text = Session["ScreenWidth"].ToString();

Open in new window


ResolutionDetector.ascx file

 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ResolutionDetector.ascx.cs" Inherits="TestScreenHeightAndWidth.Includes.Resolution" %>
<input type="hidden" name="_height" value="<%#Session["ScreenHeight"]%>" id="_height" />

<input type="hidden" name="_width" value="<%#Session["ScreenWidth"]%>" id="_width" />

<%if (_IsFirstTime){%>

<script language="javascript">

var rowURL = window.location.href;
var _objHeight = document.getElementById("_height");

var _objWidth = document.getElementById("_width"); if(_objHeight.value=="" && _objWidth.value=="")
window.location.href = window.location.href + "?_height=" + window.screen.availHeight + "&_width=" + window.screen.availWidth;
else

window.location.href = window.location.href.replace("_height="+_objHeight.value,"_height="+window.screen.availHeight).replace("_width="+_objWidth.value,"_width="+window.screen.availWidth);

</script>

<%}%>

Open in new window


ResolutionDetected.ascx - Code behind file

 
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace TestScreenHeightAndWidth.Includes
{

public partial class Resolution : System.Web.UI.UserControl

{

private bool _isFirstTime = false;public bool _IsFirstTime
{

set { this._isFirstTime = value; }

get { return this._isFirstTime; }
}

override protected void OnInit(EventArgs e)
{

if (string.IsNullOrEmpty(Request.QueryString["_height"]) || string.IsNullOrEmpty(Request.QueryString["_width"]))
{

this._IsFirstTime = true;Session["ScreenHeight"] = Session["ScreenWidth"] = string.Empty;
}

else

{

Session["ScreenHeight"] = Request.QueryString["_height"].ToString();

Session["ScreenWidth"] = Request.QueryString["_width"].ToString();
}

this.DataBind();
}

}

}

Open in new window


Usage in anywhere you want - for my example - Default aspx

 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestScreenHeightAndWidth._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register TagName="ScreenResResolver" TagPrefix="CC1" Src="~/Includes/ResolutionDetector.ascx" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

<title>Test Screen Height and Width</title> </head>
<body>

<form id="form1" runat="server">

<CC1:ScreenResResolver runat="server" ID="ScreenResResolver1"></CC1:ScreenResResolver>

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<div>

Height of your screen = <asp:Label ID="lblHeight" runat="server"></asp:Label>
and Width of your screen = <asp:Label ID="lblWidth" runat="server"></asp:Label>

</div>

</form> </body>
</html>

Open in new window


Default.aspx.cs

 
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace TestScreenHeightAndWidth
{

public partial class _Default : System.Web.UI.Page

{

 

protected void Page_Load(object sender, EventArgs e)
{

this.lblHeight.Text = Session["ScreenHeight"].ToString();

this.lblWidth.Text = Session["ScreenWidth"].ToString();
}

}

}

Open in new window



Its a complete code.

Thats all from my side

Regards,

Asif Ahmed Khan
zrazakAuthor Commented:
Can this code be implemented if mobile use a particular stylesheet?
zrazakAuthor Commented:
Any comments for this code is greatly appreciated.
khan_webguruCommented:
yup you can
zrazakAuthor Commented:
Any help with code for this is greatly appreciated
zrazakAuthor Commented:
This code gives errors in Resolution.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResolutionDetected.aspx.cs" Inherits="TestScreenHeightAndWidth.ResolutionDetected" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%if(_IsFirstTime){%>

<script language="javascript">

var rowURL = window.location.href;
var _objHeight = document.getElementById("_height");

var _objWidth = document.getElementById("_width"); if(_objHeight.value=="" && _objWidth.value=="")
window.location.href = window.location.href + "?_height=" + window.screen.availHeight + "&_width=" + window.screen.availWidth;
else

window.location.href = window.location.href.replace("_height="+_objHeight.value,"_height="+window.screen.availHeight).replace("_width="+_objWidth.value,"_width="+window.screen.availWidth);

</script>

<%}%> 


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
<input type="hidden" name="_height" value="<%#Session["ScreenHeight"]%>" id="_height" />

<input type="hidden" name="_width" value="<%#Session["ScreenWidth"]%>" id="_width" />
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


Resolution .cs get error on toggle at bottom  
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



namespace TestScreenHeightAndWidth.Includes
{

public partial class Resolution : System.Web.UI.UserControl

{

private bool _isFirstTime = false;public bool _IsFirstTime
{

set { this._isFirstTime = value; }

get { return this._isFirstTime; }
}

override protected void OnInit(EventArgs e)
{

if (string.IsNullOrEmpty(Request.QueryString["_height"]) || string.IsNullOrEmpty(Request.QueryString["_width"]))
{

this._IsFirstTime = true;Session["ScreenHeight"] = Session["ScreenWidth"] = string.Empty;
}

else

{

Session["ScreenHeight"] = Request.QueryString["_height"].ToString();

Session["ScreenWidth"] = Request.QueryString["_width"].ToString();
}

this.DataBind();
}

}

} 
Toggle HighlightingOpen in New WindowSelect All

Open in new window


Default.cs same error with toggle  
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

namespace TestScreenHeightAndWidth
{

public partial class _Default : System.Web.UI.Page

{

 

protected void Page_Load(object sender, EventArgs e)
{

this.lblHeight.Text = Session["ScreenHeight"].ToString();

this.lblWidth.Text = Session["ScreenWidth"].ToString();
}

}

} 
Toggle HighlightingOpen in New WindowSelect All

Open in new window


This is what i use as Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestScreenHeightAndWidth._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagName="ScreenResResolver" TagPrefix="CC1" Src="~/Includes/ResolutionDetector.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Test Screen Height and Width</title> </head>
<body>

<form id="form1" runat="server">

<CC1:ScreenResResolver runat="server" ID="ScreenResResolver1"></CC1:ScreenResResolver>

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<div>

Height of your screen = <asp:Label ID="lblHeight" runat="server"></asp:Label>
and Width of your screen = <asp:Label ID="lblWidth" runat="server"></asp:Label>

</div>

</form> </body>
</html>

Open in new window

zrazakAuthor Commented:
No return on answer
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.