今天在自学网上学习了一下页面之间控件的传值,经过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可用");
}
}