如果隐藏域的值不为空,表示用户单击了提交按钮发出了POST请求
if (!string.IsNullOrEmpty(Request.Form["isPostBack"]))
IsPostBack:如果是POST请求该属性的值为true,如果是GET请求该属性的值为false.
IsPostBack:是根据__VIEWSTATE隐藏域进行判断的,如果是POST请求那么该隐藏域的值会提交到服务端,那么IsPostBack属性也就为true.如果将form标签的runat="server"去掉,那么就不能用该属性进行判断是POST请求还是GET请求。因为去掉form标签的runat="server",那么就不会再产生 __VIEWSTATE隐藏域了。
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
邮箱:<input type="text" name="txtMail" /><br />
<input type="hidden" name="isPostBack" value="aaa" />
<input type="submit" value="添加用户" />
public partial class AddUserInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//如果隐藏域的值不为空,表示用户单击了提交按钮发出了POST请求
// if (!string.IsNullOrEmpty(Request.Form["isPostBack"]))
//IsPostBack:如果是POST请求该属性的值为true,如果是GET请求该属性的值为false.
//IsPostBack:是根据__VIEWSTATE隐藏域进行判断的,如果是POST请求那么该隐藏域的值会提交到服务端,那么IsPostBack属性也就为true.如果将form标签的runat="server"去掉,那么就不能用该属性进行判断是POST请求还是GET请求。因为去掉form标签的runat="server",那么就不会再产生 __VIEWSTATE隐藏域了。
if (!string.IsNullOrEmpty(Request.Form["isPostBack"]))
{
InsertUserInfo();
}
}
protected void InsertUserInfo()
{
Model.UserInfo userInfo = new Model.UserInfo();
userInfo.UserName = Request.Form["txtName"];
userInfo.UserPass = Request.Form["txtPwd"];
userInfo.Email = Request.Form["txtMail"];
userInfo.RegTime = DateTime.Now;
BLL.UserInfoService userInfoService = new BLL.UserInfoService();
if (userInfoService.AddUserInfo(userInfo))
{
Response.Redirect("UserInfoList.aspx");
}
else
{
Response.Redirect("/Error.html");
}
}
}
或者
<%-- <input type="hidden" name="isPostBack" value="aaa" />--%>
if (IsPostBack)
{
InsertUserInfo();
}