asp.net中对象失去焦点时自动提交数据 V2
.aspx页只拉一个TextBox控件:
.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);
}
}
全部评论