我有一个 asp.net 页面,上面有许多控件,包括:
autopostback = true
实现了服务器端textchanged
事件的文本框实现了服务器端点击事件的按钮
一旦用户离开控件(即失去焦点),文本框就会执行回发。问题是,如果用户碰巧更改了值,然后在不离开文本框的情况下按下按钮,那么在按钮单击时,文本框将执行回发,但按钮单击将丢失。
在这种情况下,如何强制文本框和按钮事件连续触发?
谢谢
尝试:
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)
{
}
您可以延迟文本框的回发并在单击按钮的情况下取消它。为此,将以下代码添加到Page_PreRender
方法中:
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, ""));
}