简体   繁体   中英

Request.Form empty?

i have a aspx form

<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>

I want to post the values from this form to a different page on the click event. click event code is :

   protected void btnSubmit_Click(object sender, EventArgs e)
    {


        Response.Redirect(ccpUrl);


    }

when i do a Request.form on the landing page it is EMPTY.it throws null reference exception.
here is the code for that

 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();


    }

what am i doing wrong ? Am i missing something ? please help .

Use ClientIDMode="static" . You're field names are being dynamically generated, and will not match those you have put in the ID fields.

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

You should also do the redirect with Server.Transfer to ensure the values are transferred to the subsequent page you are redirecting to. Request.Form[...] returns null when a key is not found, and the ToString() on that value will give you a NullReferenceException (since you cannot invoke a method on a null value).

Response.Redirect() only redirects the page. It doesn't forward any header information whatsoever. You'll need to set up a cross-page PostBack.

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

Should give you some guidance.

Or you can set up the button like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM