码迷,mamicode.com
首页 > 其他好文 > 详细

为什么PreviousPage为null

时间:2015-10-24 22:07:13      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:

      今天在自学网上学习了一下页面之间控件的传值,通过Button中的PostBackUrl来实现页面的跳转,通过PreviousPage.FindControl(“”);来获取控件的id,以实现页面值的传递 .  但发现previousPage一直是null,为了避免代码原因,我随手写了个button来测试了一下:

c_set.aspx为传值页

   <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/c_read.aspx"  />

 c_read.aspx为接受页

c_read.aspx.cs:

  protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            Response.Write("previousPage不是空");
        }
        else
        {
            Response.Write("previousPage是空");
        }
    }

技术分享

技术分享

显示结果为previousPage为空

最后我在网上查了很久,才找到原因:When you use the default WebForm from visual Studio, the AutoRedirectMode is set to Permanent. This makes you request into a “GET” and since you are using Friendly URLs1, you can’t evaluate the PreviousPage. 

The problem was the FriendlyUrls nuget package was removing the .aspx after my page names so my target page was not WebForm2.aspx but just WebForm2. This made the previous page null.

If you want a “POST” action then set the AutoRedirectMode = RedirectMode.Off (this will give you PreviousPage info but only from non-Friendly-Url pages [ex: www.you.com/mypage.aspx], however this will get you an error if you try to access the Friendly-Url page [ex: www.you.com/mypage] << no .aspx).

当你用建立网站的时候通过ASP.Net WEB窗体网站,那么在运行的时候浏览器会隐藏页面的后缀。

如果我们通过空网站建立WEB项目即:技术分享

那么previousPage就不会为空了。

c_read.aspx.cs:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            Response.Write("previousPage可用");
        }

    }

技术分享

技术分享

 

为什么PreviousPage为null

标签:

原文地址:http://my.oschina.net/u/2494495/blog/521636

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!