makambi
makambi

Reputation: 779

Call page_load form jquery ajax client side

I need to get user screen resolution on server side and resize controls according to resolution.

I have asp:HiddenField where I want to store resolution.

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

I have jquery ajax call that submits resolution:

 $(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"); }
     });
 });

And web method for it:

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

From WindowResolutionSet I can't access server controls on the page.

And sucess: function (msg) { alert("it works"); does not fire.

I thought to populate width and height on sucess and then access it from Page_Load() but sucessdoes not fire. error does not execute as well.

There must be more efficient way, but I don't know it =/

Upvotes: 0

Views: 940

Answers (1)

Steve B
Steve B

Reputation: 37660

These values are directly accessible using HttpCapabilitiesBase.ScreenPixelsWidth and HttpCapabilitiesBase.ScreenPixelsHeight.

Example:

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

Upvotes: 3

Related Questions