Auto repeat prevents button presses

I have an asp.net page and it has several controls, including:

  • Text field with autopostback = true and executed on server side textchanged

  • Server side click event button implemented

The text field performs postback as soon as the user leaves the control (i.e., focus is lost). The problem is that if the user accidentally changed the value, and then pressed the button without leaving the text field, then clicking the button, the text field will perform a postback, but the button will be lost.

How can I make both the text box and the button events fire sequentially in such cases?

thanks

+6
source share
2 answers

Try:

Aspx:

 <asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox> <asp:Button ID="Button1" runat="server" style="display:none" Text="Button" /> 

JS:

 function EnterEvent(e) { if (e.keyCode == 13) { __doPostBack('<%=Button1.UniqueId%>', ""); } } 

CS:

 protected void Button1_Click1(object sender, EventArgs e) { } 
+1
source

You can delay the postback from the text box and cancel it when you click the button. To do this, add the code below to the Page_PreRender method:

 protected void Page_PreRender(object sender, EventArgs e) { Button1.OnClientClick = string.Format("if(window.{0}Timeout){{ clearTimeout(window.{0}Timeout); }}", TextBox1.ClientID); TextBox1.Attributes["onChange"] = string.Format("javascript:window.{0}Timeout = setTimeout(\"{1}\", 500); return;", TextBox1.ClientID, ClientScript.GetPostBackEventReference(TextBox1, "")); } 
0
source

Source: https://habr.com/ru/post/956621/


All Articles