爲何PreviousPage爲null

      今天在自學網上學習了一下頁面之間控件的傳值,經過Button中的PostBackUrl來實現頁面的跳轉,經過PreviousPage.FindControl(「」);來獲取控件的id,以實現頁面值的傳遞 .  但發現previousPage一直是null,爲了不代碼緣由,我隨手寫了個button來測試了一下:web

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是空");
        }
    }this

顯示結果爲previousPage爲空lua

最後我在網上查了好久,才找到緣由: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. url

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.spa

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)..net

當你用創建網站的時候經過ASP.Net WEB窗體網站,那麼在運行的時候瀏覽器會隱藏頁面的後綴。

若是咱們經過空網站創建WEB項目即:

那麼previousPage就不會爲空了。

c_read.aspx.cs:

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

    }

相關文章
相關標籤/搜索