简体   繁体   English

如果页面大小小于700px,asp.net重定向到另一个URL

[英]asp.net redirect to another url if page size is less then 700px

I have a page eg desktop.aspx?customerID=345 & mobile.aspx?customerID=345 . 我有一个页面,例如desktop.aspx?customerID=345mobile.aspx?customerID=345 Both pages have same functionality. 两个页面具有相同的功能。 So supposer user goes here desktop.aspx?customerID=345 and screen size is less then 700px then it should redirect to mobile.aspx?customerID=345 . 因此,订阅者用户转到此处desktop.aspx?customerID=345并且屏幕尺寸小于700像素,则应重定向到mobile.aspx?customerID=345 Now with Jquery I can do this redirect but here I have dynamic query string as well. 现在,使用Jquery我可以执行此重定向,但是在这里,我也具有动态查询字符串。

Is it possible to detect screen size in code behind of .aspx file? 是否可以在.aspx文件后面的代码中检测屏幕大小?

To check the screen and browser width you can use javascript match media: 要检查屏幕和浏览器的宽度,可以使用javascript匹配媒体:

For browser: 对于浏览器:

var isBrowserLessThan700 = (window).matchMedia('screen and (max-width: 700px)').matches;

For the screen: 对于屏幕:

var isDeviceLessThan700 = (window).matchMedia('screen and (max-device-width: 700px)').matches;

And if result is true: 如果结果为真:

   window.loacation.href= "What-You-Want";

Solution 1:- Create a hidden field variable in the client side. 解决方案1:在客户端中创建一个隐藏字段变量。

.ASPX File:- .ASPX文件:-

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

Script:- 脚本:-

<script type="text/javascript">
 var hdncustomerID = $('hdncustomerID').Val(); //if u use Jquery

        $(document).ready(function () {
            if ($(window).width() < 700) {
                window.location = mobile.aspx?customerID=123;
            }
        });
</script>

Solution 2:- 解决方案2:-

protected void Page_Load(object sender, EventArgs e)
    {
        var CustomerID = 1;

        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script type=\"text/javascript\">");
        strScript.Append("$(document).ready(function () {");
        strScript.Append("if ($(window).width() < 700) {");
        strScript.Append("window.location = mobile.aspx?customerID='");
        strScript.Append(CustomerID);
        strScript.Append("';");
        strScript.Append("}");
        strScript.Append(" });");
        strScript.Append("</script>");

        ClientScriptManager script = Page.ClientScript;
        script.RegisterClientScriptBlock(this.GetType(), "redirect", strScript.ToString());

    }

Hope Its Work !!! 希望它的工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要一个jQuery函数才能仅在700px屏幕尺寸以上工作,无法在我的代码中发现错误 - I need a jQuery function to only work above 700px screen size, cant spot the error in my code 当屏幕宽度&gt; 700像素时隐藏JavaScript吗? - Hide JavaScript when screen width > 700px? 使用 javascript 时,如何只让我的 header 菜单在屏幕尺寸大于 700 像素时缩小? - How can I only let my header menu shrink when the screen size is larger than 700px by using javascript? 将Google Maps InfoWindow设置为超过700像素的宽度 - Set google maps InfoWindow to more than 700px width 如何在ASP.NET中进行请求期间重定向到另一个页面 - How to redirect to another page during request in ASP.NET 使用javascript函数从asp.net页面重定向到另一个页面 - Redirect from asp.net page to another using javascript function Asp.Net:在更新面板中从用户控件重定向到另一个页面 - Asp.Net : Redirect to another page from usercontrol in update panel 使用javascript在700px之后显示div后,如何隐藏它? - after using javascript to show a div after 700px how do i hide it? 在屏幕 700 像素 html/css/js 前端 Web 开发后不显示汉堡菜单 - Hamburger Menu not showing after screen 700px html/css/js front-end web dev 如何使用 Jquery 检查 div 的高度是否大于 700px? - How do I check if the height of a div is greater than 700px, using Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM