2

I need a value from javascript before the main page load and then want to use that value in code. The code which I am using for that purpose is:

I have make a test.aspx page. The code for which is as following:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title> <script type="text/javascript" language="javascript">   
function GetScreenHeightAndWidth()

 {   
        var width = screen.width;   
        var height = screen.height;
        var object = 'Label1';

document.getElementById(object ).innerHTML=height ;
//alert(height);

'<%Session["Screensize"] = "' + height +'"; %>' ;

    }   

    </script>  
</head>
<body onload="GetScreenHeightAndWidth();" >
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

The code for test.aspx.cs page is as following:-

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;
using System.IO;
using System.Windows.Forms;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Screensize"] = Label1.Text;
        TextBox1.Text=Session["Screensize"].ToString();

    }
}

The result is as following:-

768 test

while result what I need is 768 and 768.

Is there any way to solve the problem?

3
  • 6
    That's just not possible. JavaScript only runs, on the client, after the c# code has already finished running, on the server. Commented Apr 18, 2012 at 14:08
  • In this case is there any solution for this problem Commented Apr 18, 2012 at 14:11
  • @JohnSaunders: You are a stickler for using tags :) Commented Apr 18, 2012 at 14:13

3 Answers 3

3

What you are asking is impossible to do. Why do you need to know the screen size on the server -side in the first place? Whatever you need to accommodate on the html produced by the server can be either adjusted via proper CSS rules or Javascript (JQuery if you prefer that framework)

Sign up to request clarification or add additional context in comments.

Comments

1

If you set the width/height values to a hidden field from script, you can than access them from codebehind after a postback. Labels are not posted with the form so you cant use that.

Comments

0

First of all thanks to all of you for responding to my question. After much search I have found what I was exactly wanting. For this I have to use one more page and add this code in my original code:

if (!IsPostBack)
        {
            if (Session["ScreenResolution"] == null)
            {
                Response.Redirect("detectscreen.aspx");
            }
            else
            {
                Session["Screensize"] = Session["ScreenResolution"].ToString();
            }



            TextBox1.Text = Session["Screensize"].ToString();
        }

And the code for new page is:

 protected void Page_Load(object sender, EventArgs e)
    {
         if (Request.QueryString["action"] != null) {

            Session["ScreenResolution"] = Request.QueryString["res"].ToString();
            Response.Redirect("Catalogue.aspx");
        }
    }

and the script in the new page will be like this:

 <script language="javascript" type="text/javascript">
    res = "&res="+screen.height
    top.location.href="detectscreen.aspx?action=set"+res

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.