2

メインページをロードする前に JavaScript からの値が必要で、その値をコードで使用したいと考えています。その目的で使用しているコードは次のとおりです。

test.aspx ページを作成しました。コードは次のとおりです。

<%@ 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>

test.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;
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();

    }
}

結果は次のとおりです。

768テスト

結果として必要なのは768と768です。

問題を解決する方法はありますか?

4

3 に答える 3

3

あなたが求めていることは不可能です。そもそもサーバー側の画面サイズを知る必要があるのはなぜですか? サーバーによって生成された html に対応する必要があるものはすべて、適切な CSS ルールまたは Javascript (そのフレームワークを好む場合は JQuery) を介して調整できます。

于 2012-04-18T14:11:01.457 に答える
1

幅/高さの値をスクリプトから非表示のフィールドに設定すると、ポストバックの後にコードビハインドからそれらにアクセスできます。ラベルはフォームに掲載されていないため、使用できません。

于 2012-04-18T14:14:08.267 に答える
0

まず、私の質問に答えてくれた皆さんに感謝します。多くの検索の後、私はまさに私が欲しかったものを見つけました. このために、もう 1 ページ使用して、元のコードに次のコードを追加する必要があります。

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



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

新しいページのコードは次のとおりです。

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

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

新しいページのスクリプトは次のようになります。

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

</script>
于 2012-04-19T12:48:58.567 に答える