日期:2014-05-18  浏览次数:20502 次

请问如何在页面一打开时,使TextBox1获得焦点?
请问各位大师:

1、如何在页面一打开时,使TextBox1获得焦点?
例如像hao123.com上的百度搜索的输入框那样。

2、如何在完成某一项操作后,指定某个TextBox获得焦点?

多谢!

------解决方案--------------------
window.onload=function()
{
document.getElementById( ' <%=TextBox1.ClientID%> ').focus();//2003document.getElementById( 'TextBox1 ').focus()
}
------解决方案--------------------

在后台写上如下代码:
public void GetFocus(string str_ctrl_name,Page page)
{
page.RegisterStartupScript( " ", " <script> document.forms(0). "+str_ctrl_name+ ".focus();document.forms(0). "+str_ctrl_name+ ".select(); </script> ");
}
然后在需要获得焦点的地方调用此函数就可以了
例如让TextBox1获得焦点,则可以如下写:
GetFocus( "TextBox1 ",this.Page);
------解决方案--------------------
Response.Write(string.Format( " <script> window.onload=function(){document.getElementById( '{0} ').focus();} </script> ",TextBox1.ClientID));

在后台写