简体   繁体   中英

ASP.NET website - changing the value entered in a form before submitting

I have a form in a .aspx file in a Visual Studio project. I have a variable that I want to change the value of before the user hits the submit button. I cannot figure out how to do this in Visual Studio. I am new to Visual Studio and when I am developing for Android, it is easy: just make the changes in the OnClick method.

I created a function in the code behind and I have that as the event handler for the submit button. The problem is, when the submit button redirects, the value is unchanged.

Field:

<input type=hidden name="AMOUNT" value="<%=amount%>">

Submit button:

<asp:Button ID="btnComplete" type="submit" runat="server" Text="Submit" OnClick="Submitted" />

Submitted function:

protected void Submitted(Object sender, EventArgs e)
{
    amount += 1;
}

The "amount" variable is 100 and is displayed as 100 on the next page instead of 101.

I have tried this and it is working fine. I suggest you debug your page with F11 to see which line changes your variable value.

<asp:Button ID="btnComplete" type="submit" runat="server" Text="Submit" OnClick="Submitted" />
<input  name="AMOUNT" value="<%=amount%>" />

And;

protected int amount = 100;
protected void Submitted(object sender, EventArgs e)
{
    if (Session["amount"] != null) amount = (int)Session["amount"];
    amount += 1;
    Session["amount"] = amount;
}

我最后只是发布到同一页面,检查发布数据并更改值,然后再将其发送到其他URL。

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