使用asp.net的ajax后,导致我设置的Textbox的focus失效,如何解决?
首次进入页面时,TextBox所在的focus是正常的,但当我点击按钮进行回传处理时,使用了ajax不刷新页面。但是我无论在哪设置前台的TextBox控件focus,都无效。焦点没在TextBox中。有办法解决吗?
运行ScriptManager.RegisterStartupScript(UpdatePanel1,this.GetType(), "message1 ", "document.getElementById( "TextBox1 ").focus(); ",true);
也不行!
代码如下:
前台:
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:ScriptManager ID= "ScriptManager1 " runat= "server ">
</asp:ScriptManager>
<asp:UpdatePanel ID= "UpdatePanel1 " runat= "server ">
<ContentTemplate>
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>
<asp:TextBox ID= "TextBox2 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
后台:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Focus();//初始是正常
}
else
{
//TextBox1.Focus();//回发后就不行了
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Focus();//这样写不行,换上下面这样也不行!
ScriptManager.RegisterStartupScript(UpdatePanel1,this.GetType(), "message1 ", "document.getElementById( "TextBox2 ").focus(); ",true);
}
}
------解决方案-