How come form.reset() doesn't work after postb

2020-04-11 10:03发布

问题:

A student in my class brought this to my attention, and I didn't have an explanation for it-- and I couldn't find one after searching.

After clicking the submit button, the reset button does not work. It's as if the browser prevents reset if the resource has been requested via post request.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <legend>User Form</legend>
            <label for="Username"></label>
            <asp:TextBox ID="Username" runat="server"></asp:TextBox>
        </fieldset>
        <asp:Button runat="server" Text="Submit" />
        <input type="button" onclick="window.document.forms[0].reset()" value="reset" />
    </div>
    </form>
</body>
</html>

回答1:

When you click reset then the entries are reset to the default values. Default values are values which were rendered by the server in the first case it was empty string but after post the posted value. (http is stateless protocol) If you try to edit the posted value then click reset you will see that the originally posted value will be restored.