请求.表单是空的(asp.net c#)
本文关键字:asp net 表单 请求 | 更新日期: 2023-09-27 18:08:56
我一直在用ASP做一些非常聪明的事情(我认为)。Net c#,如此之多,以至于简单的东西更困难(如果这有意义的话)
在我的页面
中有这段代码<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
我在confirm.aspx
中有这段代码。
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
我期待这是很好和简单,但当我点击按钮,去页面"确认。aspx"的请求。形式没有价值。我错过了什么?
[测试]我在VS2013中对一个全新的web表单项目进行了测试。点。Net 4.5.1这不起作用。PreviouPage
总是空的。是否被(!IsPostBack)
包围。提交控件是Button
、LinkButton
还是Hyperlink
都无关紧要。Request.Form["hdn"]
也是null。为了以防万一,我重新启动了电脑,但还是没有任何乐趣。我错过了一些非常简单的东西,我确信它,但我看不到
这是第一页
后面没有任何代码<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
第二页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
背后的代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
在confirm.aspx
上使用PreviousPage.FindControl
:
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
这是怎么回事:
默认情况下,VS 2013和Asp。Net v4.5.1中有一个特性 FriendlyUrls
是启用的。
FriendlyUrls特性做了一些Asp。Net路由,从而将url从localhost/webform1.aspx
更改为localhost/webfrom1
PostbackUrl
属性将不能与Asp组合使用。网络路由。在ASP。. NET路由正在使用中,PreviousPage
URL是最终路由的URL。运行时现在将检查由路由URL表示的文件,它将是webform1
而不是webfrom1.aspx
。因为没有webform1
这样的文件,所以它总是将PreviousPage
设置为null
。
可能的解决方式:
那么,现在你知道手头的问题了。
1)。或者不要使用Asp的Routing系统。因此,在本例中,请尝试将<%@ PreviousPageType VirtualPath="~/WebForm1.aspx"%>
添加到webform2.aspx
页面并检查。
2)。或者,如果您保留了FriendlyURLs并因此保留了路由系统,则可以使用其他替代方法更改代码以读取Form值。