0

概括

通过 asp.net 4.0 中的按钮事件翻转母版页时,我遇到了样式问题。新的 master 切换,但旧 master 的 css 仍然存在。我不明白这是怎么发生的,因为样式是在旧主人的头脑中定义的,我可以通过标记清楚地看到新主人正在显示的应该是一组完全不同的样式。此外,查看源代码会在头部显示所有新的 css 声明。我怎样才能让它“刷新”或“重新加载”?

一些细节

我正在实现我的 asp.net 网站的移动版本。如果检测到移动设备,我会设置一个 cookie 并将 preinit 中的母版页切换到移动设备友好的页面。这工作正常:

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 Mobile Scripts 有关。JQuery Mobile 有这种将页面绑定在一起的方式。我不完全理解它,我认为他们将它用于页面转换,并且它阻止了我的新 CSS 注册。当我关闭它时,我的母版页可以正常翻转并包含 css。我正在寻找一种在重定向之前关闭 JQuery Mobile 的方法。请注意确定如何。

4

1 回答 1

1

该问题最终与页面转换的 JQuery Mobile AJAX 有关。JQuery Mobile 不会在第一次之后的附加页面请求上加载文档的头部。

因此,当我将移动主机切换到桌面主机时,文档的头部不会加载以引入我的样式。有几种方法可以解决这个问题:

这种方式只是完全关闭 AJAX,并解决了问题,但你不能从中受益:

<form data-ajax="false">

这是一种有问题的方法,但请提醒您,在 JQuery Mobile 初始化后,它将无法通过事件工作,因此您也无法从中受益:

$.mobile.ajaxEnabled = false;

如果您必须使用 onclick 事件和事件处理程序首先通过页面重定向,我支持的上述两种解决方案可能会起作用。

更好的解决方案是在链接中添加 rel="external" 以告诉 JQM 它是和传出链接。

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

但是因为我无法运行一些我想要更改 cookie 的代码,所以我必须传递一个查询字符串参数,在 preinit 上检查它,然后设置我的页面也在 preinit 上查看的 cookie 并翻转掌握。

这是我下面的完整解决方案,以防有人在外面做完全相同的事情。请注意,因为我的网站使用别名,所以我必须阅读 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;
    }

然后我只是使用我的字典对象来评估和重建我的 url 与相反的移动值,动态设置切换链接上的 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 上,这是我实际检查的地方,首先是查询字符串,然后是 cookie,如果它们都不存在,我运行我的移动检测方法,并设置一个 cookie,以及我的接口 bool 属性,以便轻松访问条件取决于它。

        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;
    }
于 2013-03-08T20:04:26.620 回答