ViewState用于保存服务器控件(runat="server")状态便于在触发回调后(PostBack)恢复页面。
页面拖入一个Form id="form1" runat="server",里面添加一个<asp:Button id="Button1" Text="删除" 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.点击按钮后台Request["__EVENTTARGET"]为null,给Button添加UseSubmitBehavior="false"即可。此时查看客户端代码可以看到自动在form元素下添加了一下内容:
<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.input submit点击时会将自身的name值作为表单域提交
点击重置:
点击查询:
查询调用删除:
3.设置“__EVENTTARGET”表单域可以出发后台相应控件的事件。文章来源:https://www.toymoban.com/news/detail-605187.html
比如__doPostBack("Button1","")会触发Button1_Click事件文章来源地址https://www.toymoban.com/news/detail-605187.html
到了这里,关于Asp.Net WebForm ViewState的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!