asp.net中对象失去焦点时自动提交数据 V2

时间就是生命。时光不会倒流,珍惜此刻,奋斗未来吧。记住你无畏青春的样貌,你未必出类拔萃,但必须与众不一样。
.aspx页只拉一个TextBox控件:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>

.aspx.cs页中,首选在Page_Init事件,为TextBox注册OnBlur事件:
 
protected void Page_Init(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("onblur", Page.ClientScript.GetPostBackEventReference(this.TextBox1, "OnBlur"));
}

写一个onBlue事件,将替代LinkButton的Click事件:
 
private void OnBlurHandle(string ctrl, string args)
{
if (ctrl == this.TextBox1.UniqueID && args == "OnBlur")
{
//这里写提交到数据库中
}
}

然后在网页的Page_Load事件,判断是否IsPostBack。
 
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var ctrl = Request.Params[Page.postEventSourceID];
var args = Request.Params[Page.postEventArgumentID];
OnBlurHandle(ctrl, args);
}
}

到此这篇关于asp.net中对象失去焦点时自动提交数据 V2就介绍到这了。三十年河东,三十年河西,莫欺少年穷!更多相关asp.net中对象失去焦点时自动提交数据 V2内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

asp.net中MVC的处理流程详解

ASP.NET Core的日志系统介绍