请求.表单为空(asp.net c#)

请求.表单为空(asp.net c#),c#,asp.net,C#,Asp.net,我在ASP.NETC#中做了一些非常聪明的事情(我想),以至于简单的事情变得更加困难(如果有意义的话) 我的页面中有这段代码 <form id="form1" runat="server"> <asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" /> <asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" T

我在ASP.NETC#中做了一些非常聪明的事情(我想),以至于简单的事情变得更加困难(如果有意义的话)

我的页面中有这段代码

<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”页面时,请求。表单没有值。我错过了什么

[测试]

我在VS2013中对一个全新的web表单项目进行了测试。Dot.Net 4.5.1这不起作用
PreviouPage
始终为空。是否被
(!IsPostBack)
包围。无论提交控件是
按钮
链接按钮
还是
超链接
<代码>请求。表单[“hdn”]也为空。为了以防万一,我重新启动了电脑,但还是没有什么乐趣。我错过了一些非常简单的东西,我确信这一点,但我看不出是什么

这是第一页,代码中没有任何内容

<%@ 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>
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功能执行某种Asp.Net路由,从而将URL从
localhost/webform1.aspx
更改为
localhost/webfrom1

PostbackUrl
属性不能与Asp.Net路由结合使用。使用ASP.NET路由时,
PreviousPage
URL是最终路由的URL。运行时将立即检查由路由URL表示的文件,该文件将是
webform1
,而不是
webfrom1.aspx
。由于没有像
webform1
这样的文件,因此它总是将
上一页
设置为
null

可能的解决方案:

所以,现在你知道了眼前的问题

1.)或者不要使用Asp.Net友好URL的路由系统,在这种情况下,请尝试将
添加到
webform2.aspx
页面并进行检查


2.)或者,如果您保留了FriendlyURL和路由系统,则可以使用其他替代方法更改代码以读取表单值

应该说我试过了,但也没用。我以前做过这件事,我检查过我的其他代码,看不到任何不同。[UPDATE]PreviousPage是空的time@user985197我只是在一个空的解决方案中尝试了上面的代码,效果非常好,我得到了“Hello World”作为hiddenfield的值——我在LoadSee中调用了上面的代码查看我的评论和更新的问题。这不起作用。
如果!(IsPostback)
的语法无效。即使假设您的意思是
如果(!IsPostback)
,那么这只适用于第一页条目,而不适用于回发?您确定该控件在呈现的HTML中被称为
hdnConfirm
?您是否尝试过使用
Request.Form[hdnConfirm.UniqueID]
替代?使用浏览器中的开发工具检查post请求。验证发布的数据是否存在,以及字段是否具有您期望的名称(“hdnConfirm”)。我猜HiddenField的名字根本不是“HDN公司”。名称才是最重要的。您能用
lblConfirm.Text=Request.Form.AllKeys.Aggregate(“,(当前,键)=>current+(“+key+”:“+Request.Form[key]+”

)检查
Request.Form
中的内容吗也这么做了。集合中没有任何内容,它是空的,快速观察显示
AllKeys{string[0]}string[]
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;
        }
    }
}
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
    hiddenValue = hdnFieldName.Value;
}