Asp.Net WebForm ViewState

ViewState is used to save the state of the server control (runat="server") to restore the page after the callback is triggered (PostBack).

Drag a Form id="form1" runat="server" into the page, and add a <asp:Button id="Button1" Text="Delete" OnClick="Button1_Click" OnClientClick="return checkDel(this)" />

<form id="form1" class="pure-form" name="form1" runat="server">
    <input type="text" id="txtName" name="Name" placeholder="名称" size="10"/>
    <input name="Reset" type="submit" value="重置" />
    <input name="Search" type="submit" value="查询" />
    <button name="Button1" type="button" class="pure-button pure-button-primary" onclick="doSearch()">查询</button>
    <asp:Button runat="server" UseSubmitBehavior="false" ID="Button1" OnClick="Button1_Click" class="pure-button pure-button-primary" Text="删除" />
</form>
<label runat="server" id="lblFormKeys"></label><br />
<label runat="server" id="lblEventTarget"></label>
<script src="/js/jquery-3.6.4.min.js"></script>
<script>
    function doSearch() {
        var btn = $("#txtName").val() || "Button1";
        __doPostBack(btn, "");
    }
</script>

 

1. Click the button and the background Request["__EVENTTARGET"] is null, just add UseSubmitBehavior="false" to the Button. At this point, if you look at the client code, you can see that the content is automatically added under the form element:

<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="" />
</div>

2. When input submit is clicked, its own name value will be submitted as a form field

Click Reset:

 Click to query:

Query calls delete:

3. Set the "__EVENTTARGET" form field to trigger the event of the corresponding control in the background.

For example, __doPostBack("Button1","") will trigger the Button1_Click event

Guess you like

Origin blog.csdn.net/zhchfsky/article/details/131877268