简体   繁体   中英

asp.net submitting before setting the hidden values

I have an html form with hidden values empty like below

<body>
<form runat="server" id="PostToMPI" name="PostToMPI" method="post" action="https://www.e-tahsildar.com.tr/V2/NetProvOrtakOdeme/NetProvPost.aspx" >

    <asp:HiddenField ID="pHashB64" runat="server" Value="" />
    <asp:HiddenField ID="pHashHex" runat="server" Value="" />

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

in the c#

protected void Button1_Click(object sender, EventArgs e)
{
   pHashB64.Value = "calculated value";
   pHashHex.Value = "calculated value";
}

it is using post method. when the user clicks the button, I am calculating the values set them to the hidden fields.

I am wondering if it submits the form before it sets the hidden fields? I mean that I am posting with empty fields?

thank you

When you specify action attribute in form tag, it transfers your request to that URL instead of firing postback in the same page and executing button click event.

Rather you can use querystring method whose URL will be generated in button click event and redirecting to the URL set in action attribute.

<form runat="server" id="PostToMPI" name="PostToMPI" method="post" >

<asp:HiddenField ID="pHashB64" runat="server" Value="" />
<asp:HiddenField ID="pHashHex" runat="server" Value="" />

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

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