asp.net automatically submits data when an object loses focus
- 2020-05-17 05:22:45
- OfStack
.aspx page pulls only one TextBox control:
In the.aspx.cs page, Page_Init event is preferred, OnBlur event is registered for TextBox:
Write 1 onBlue event to replace Click event of LinkButton:
Then in the Page_Load event on the page, determine if IsPostBack.
<%@ 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>
In the.aspx.cs page, Page_Init event is preferred, OnBlur event is registered for TextBox:
protected void Page_Init(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("onblur", Page.ClientScript.GetPostBackEventReference(this.TextBox1, "OnBlur"));
}
Write 1 onBlue event to replace Click event of LinkButton:
private void OnBlurHandle(string ctrl, string args)
{
if (ctrl == this.TextBox1.UniqueID && args == "OnBlur")
{
// Here write commit to the database
}
}
Then in the Page_Load event on the page, determine if 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);
}
}