2013-01-10 59 views
0

我需要在服務器端獲取用戶屏幕分辨率,並根據分辨率調整控件大小。我有asp:HiddenField我想存儲分辨率。調用page_load表單jquery ajax客戶端

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

我有jQuery的AJAX調用提交決議:

$(document).ready(function() { 
     width = screen.width; 
     height = screen.height; 
     $.ajax({ 
     url: "Questionnaire.aspx/WindowResolutionSet", 
     data: "{ 'width': '" + width + "', 'height' : '" + height + "'}", 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     sucess: function (msg) { alert("it works"); }, 
     error: function() { alert("error"); } 
    }); 
}); 

而Web方法吧:

[WebMethod] 
    public static bool WindowResolutionSet(string width, string height) 
    { 
     return true; 
    } 

從WindowResolutionSet我無法訪問網頁上的服務器控件。

sucess: function (msg) { alert("it works");不會觸發。

我想以填充sucesswidthheight,然後從Page_Load()訪問,但sucess不火。 error也不能執行。

必須有更有效的方式,但我不知道它=/

回答

3

這些值是使用HttpCapabilitiesBase.ScreenPixelsWidthHttpCapabilitiesBase.ScreenPixelsHeight直接訪問。

例子:

var clientSize = new { 
     X = Page.Request.Browser.ScreenPixelWidth, 
     Y = Page.Request.Browser.ScreenPixelHeight 
     }; 
SomeMethod(clientSize.X, clientSize.Y); 
+0

它的答案),但仍好奇如何通過jQuery的AJAX做) – makambi

+0

事實上,如果沒有這種性質,我想你會有種砍來執行。您必須調用此webmethod,在視圖狀態或用戶會話中存儲檢測到的分辨率。然後重新加載整個頁面以再次觸發page_load事件,您可以在其中恢復視圖狀態或會話變量。沒有這個屬性那麼簡單。 –

+0

我會用你的方法,謝謝)這種好奇心只是出於教育的原因) 會在空閒時間做 – makambi