2013-03-05 61 views
0

摘要

我有作風問題通過一個按鈕事件在asp.net 4.0翻轉母版頁時。新的主交換機,但老主人的CSS仍然存在。我不明白這是如何發生的,因爲風格是在老主人的頭上定義的,我可以通過標記清楚地看到新主人正在展示什麼應該是一個完全不同的風格。此外,查看源顯示頭部中的所有新的CSS聲明。我怎樣才能得到這個「刷新」或「重新加載」?asp.net移動/桌面網站切換按鈕,切換母版,但風格「卡住」

一些細節

我執行我的asp.net網站的移動版本。如果檢測到移動設備,我設置一個cookie並將預設中的母版頁切換到適合移動設備的頁面。這工作正常:

protected virtual void Page_PreInit(Object sender, EventArgs e) 
{ 
    if (IsMobile) 
     this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile; 
} 

我有一個「完整的網站」按鈕在底部,允許你來回移動之間的移動和桌面視圖。點擊它時,我更改cookie中的值。然後,當頁面重定向到自身時,將檢查該值,並給出相應的主頁面。這也「工作」,我可以告訴正確的主頁是通過標記呈現。即使在顯示桌面主機時,仍然保留移動版本的樣式。我做了重定向,認爲它會阻止這一點。

// desktop/mobile site toggle button click event 
protected void viewMobileButton_Click(Object sender, EventArgs e) 
{ 
    HttpCookie isMobileCookie = Cookies.snatchCookie("isMobile"); 

    if (bool.Parse(isMobileCookie.Value)) 
     Cookies.bakeCookie("isMobile", "false"); 
    else 
     Cookies.bakeCookie("isMobile", "true"); 

    Response.Redirect(Request.RawUrl); 
} 

這是我第一次做這樣的事,而且不知道,如果我連去了解它的正確方式,或如何從這裏進行調試。預先感謝您的幫助。

編輯

好的,所以我想通了它與JQuery的手機腳本有關。 JQuery Mobile有這種將頁面捆綁在一起的方式。我不完全理解它,我認爲他們使用它來進行頁面轉換,並且阻止了我的新CSS註冊。當我關閉它時,我的主頁翻轉得很好,包括CSS。我正在研究一種在重定向之前關閉JQuery Mobile的方法。請注意,雖然如此。

回答

1

這個問題最終與JQuery Mobile AJAX有關的頁面轉換有關。 JQuery Mobile在第一次之後不會在其他頁面請求上加載文檔的頭部。

因此,當我將移動主機切換到桌面主機時,文檔的頭部不會加載以引入我的樣式。有幾個方式就是這種可以固定:

這樣只是關閉AJAX乾脆,並解決了這個問題,但你不能從中受益:

<form data-ajax="false"> 

這是一個辦法做到如果通過重定向

$.mobile.ajaxEnabled = false; 

以上兩種解決方案,我支持可以工作:它有問題,而是提醒你,它不會通過事件jQuery Mobile的初始化工作後,如此反覆,你不能從中受益如果您必須使用onclick事件和事件處理函數,請先查看頁面。

更好的解決方案是將rel =「external」添加到鏈接,以告知JQM它是傳出鏈接。

<a href="myself.com?mobile=true" rel="external" > 

而是因爲我無法運行一些代碼,我想以更改cookie,我必須通過查詢字符串參數,檢查它的preinit,然後將它在我的網頁看起來也餅乾在預先準備好並翻轉主人。

下面是我的完整解決方案,以防有人在那裏做同樣的事情。請注意,因爲我的網站使用別名,我不得不讀取Request.RawUrl並自己解析它,因爲Request.QueryString對象不包含我傳遞的值。

// reusable function that parses a string in standard query string format(foo=bar&dave=awesome) into a Dictionary collection of key/value pairs 
    // return the reference to the object, you have to assign it to a local un-instantiated name 
    // will accept a full url, or just a query string 
    protected Dictionary<string, string> parseQueryString(string url) 
    { 
     Dictionary<string, string> d = new Dictionary<string, string>(); 

     if (!string.IsNullOrEmpty(url)) 
     { 
      // if the string is still a full url vs just the query string 
      if (url.Contains("?")) 
      { 
       string[] urlArray = url.Split('?'); 
       url = urlArray[1]; // snip the non query string business away 
      } 

      string[] paramArray = url.Split('&'); 

      foreach (string param in paramArray) 
      { 
       if (param.Contains("=")) 
       { 
        int index = param.IndexOf('='); 
        d.Add(param.Substring(0, index), param.Substring(++index)); 
       } 
      } 
     } 
     return d; 
    } 

然後我就用我的字典對象評估和重建我的網址與對面的移動價值,動態地設置肘杆的href。一些代碼明顯被遺漏了,但是爲了透視,base._iPage.QueryStringParams保存了我返回的字典對象,而base._iPage.IsMobile只是一個布爾屬性,我也通過我使用的頁面接口,我的所有頁面,和用戶控制等,可以交談。

 // get the left side fo the url, without querystrings 
     StringBuilder url = new StringBuilder(Request.RawUrl.Split('?')[0]); 

     // build link to self, preserving query strings, except flipping mobile value 
     if (base._iPage.QueryStringParams.Count != 0) 
     { 
      if (base._iPage.QueryStringParams.ContainsKey("mobile")) 
      { 
       // set to opposite of current 
       base._iPage.QueryStringParams["mobile"] = (!base._iPage.IsMobile).ToString(); 
      } 

      int count = 0; 
      url.Append('?'); 

      // loop through query string params, and add them back on 
      foreach (KeyValuePair<string, string> item in base._iPage.QueryStringParams) 
      { 
       count++; 
       url.Append(item.Key + "=" + item.Value + (count == base._iPage.QueryStringParams.Count ? "" : "&")); 
      } 
     } 

     // assign rebuild url to href of toggle link 
     viewMobileButton.HRef = url.ToString(); 
    } 

然後在我的pageinit這是其中i實際檢查中,首先,quesry字符串,則該cookie時,如果這兩個時間都不存在,我運行我的移動檢測方法,和設置cookie,以及我的接口布爾財產易於訪問依賴它的條件。

 QueryStringParams = base.parseQueryString(Request.RawUrl); 

     if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("true") : false) 
     { 
      Cookies.bakeCookie("isMobile", "true"); // create a cookie 
      IsMobile = true; 
     } 
     else if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("false") : false) 
     { 
      Cookies.bakeCookie("isMobile", "false"); // create a cookie 
      IsMobile = false; 
     } 
     else 
     { 
      IsMobile = base.mobileDetection(); 
     } 

     if (IsMobile) 
      this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile; 
    }