2013-03-05 81 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; 
    }