[英]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=345
和mobile.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.