【问题标题】:Request.Form empty?Request.Form 为空?
【发布时间】:2015-05-18 21:54:51
【问题描述】:

我有一个 aspx 表单

<form id="form1" runat="server" method="post">   
<div>
    <asp:Panel ID="Panel1" runat="server" Width="362px">
        <asp:Table ID="Table1" runat="server" CellPadding="5">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Label ID="Label1" runat="server" Text="UserName :"></asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="userName" runat="server"></asp:TextBox>
                </asp:TableCell>
            ----- SOME CODE HERE---
        </asp:Table>
        <asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" style="height: 29px" />
    </asp:Panel>
</form>

我想将此表单中的值发布到点击事件的不同页面。 点击事件代码为:

   protected void btnSubmit_Click(object sender, EventArgs e)
    {


        Response.Redirect(ccpUrl);


    }

当我在登录页面上执行 Request.form 时,它是 EMPTY。它会引发空引用异常。
这是代码

 private void GetRequestData()
    {

            _userName = HttpContext.Current.Request.Form["userName"].ToString();
            _password = HttpContext.Current.Request.Form["password"].ToString();
            _transactionID = HttpContext.Current.Request.Form["transactionID"].ToString();
            if (Request.Form["sequenceNumber"] != null)
            {
                int.TryParse((HttpContext.Current.Request.Form["sequenceNumber"].ToString()), out _sequenceNumber);
            }
            else
                _sequenceNumber = 0;
            _tpsSystem = HttpContext.Current.Request.Form["tpsSystem"].ToString();


    }

我做错了什么? 我错过了什么吗?请帮忙。

【问题讨论】:

    标签: c# asp.net http-post


    【解决方案1】:

    Response.Redirect() 只重定向页面。它不会转发任何标题信息。您需要设置跨页 PostBack。

    https://msdn.microsoft.com/en-us/library/ms178139(v=vs.140).aspx 或者 http://www.codeproject.com/Tips/604553/Postback-and-Cross-Page-Posting-in-ASP-NET

    应该给你一些指导。

    或者你可以这样设置按钮:

    <asp:Button ID="btnSubmit" runat="server" Text="Login" PostBackUrl="/page2.aspx" style="height: 29px" />
    

    【讨论】:

      【解决方案2】:

      使用ClientIDMode="static"。您的字段名称是动态生成的,与您在 ID 字段中输入的名称不匹配。

      <asp:TextBox ID="userName" runat="server" ClientIDMode="Static"></asp:TextBox>
      

      您还应该使用Server.Transfer 进行重定向,以确保将值传输到您要重定向到的后续页面。 Request.Form[...] 在未找到键时返回 null,并且该值上的 ToString() 将为您提供 NullReferenceException(因为您不能在 null 值上调用方法)。

      【讨论】:

      • 仍然抛出 NullReferenceException 并且表单对象为空{}
      • 您是否使用了 Server.Transfer 而不是 Response.Redirect?
      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 2012-02-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多