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