一、test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication4.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 400px;">
<tr>
<td>AutoPostBack=True</td>
<td>
<asp:TextBox ID="TextBox1_True" runat="server" AutoPostBack="true"></asp:TextBox></td>
</tr>
<tr>
<td>AutoPostBack=False</td>
<td>
<asp:TextBox ID="TextBox2_False" runat="server" AutoPostBack="false"></asp:TextBox></td>
</tr>
<tr>
<td>AutoPostBack不设置</td>
<td>
<asp:TextBox ID="TextBox3_NoSet" runat="server"></asp:TextBox></td>
</tr>
</table>
</div>
</form>
<div style="color:blue;width:400px;">
AutoPostBack 属性用于设置或返回当用户在 TextBox 控件中按 Enter 或 Tab 键时,是否发生自动回传到服务器的操作。
如果把该属性设置为 TRUE,则启用自动回传,否则为 FALSE。默认是 FALSE。
</div>
</body>
</html>
二、test.aspx.cs
using System;
using System.Web.UI;
namespace WebApplication4
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
string controlNmae = Page.Request.Params["__EVENTTARGET"];
Response.Write("有回传产生!"+ controlNmae);
}
}
}
}
三、实际测试结果:
蓝色字体为w3cschool 说明, 但实际测试, 只有回车才有效果,Tab键没有。
那 AutoPostBack =True 有没有实际用处呢?
如果.net控件上没有事件,那坚决不要设置,因为会导致不必要的回传。
但如果有事件,没有配置 AutoPostBack =True 的话会有些情况下导致事件无效。