这两个问题的本质是一样的,一般是借助客户端脚本来解决,举例如下。
C#
<%@ Page Language="C#" %>
<script runat="server">
protected void Button_Click(object sender,EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Lable1.Text="您点击了:"+((Button)sender).Text;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void Page_Load(object sender,EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int TextBoxNum=4;
for(int i=1;i<=TextBoxNum;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(i!=TextBoxNum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((TextBox)form1.FindControl("TextBox"+i.ToString())).Attributes.Add("onkeydown","TabNext(event,'0','"+((TextBox)form1.FindControl("TextBox+(1+i).ToString())).ClientID+'")");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((TextBox)form1.FindControl("TextBox"+i.ToString())).Attributes.Add("onkeydown","TabNext(event,'"+Button2.ClientID+'",)");
}
}
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在页面中有一个form1的表单,4个TextBox,还有一个BUtton2按钮,一个Label1
在页面中添加下列javascript脚本:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript" type="text/javascsript">
function TabNext(e,s1,s2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(window.event)//ie
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
keynum=e.KeyCode
}
else if(e.which)//netscape,firefox,opera
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
keynum=e.which
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(keynum==13)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(s1=="0")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.getElementById(s2).focus()
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
docuemnt.getElementById(s1).click()
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(window.event)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.returnValue=false;
e.cancelBubble=true;
}
else if(e.which)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.rreventDefault()
}
}
}
</script> 记得要把button2的onclick的onclick事件绑定到button_click上。
上面的内容摘自 孟子的《ASP.NET 2.0 应用开发技术》
PS: 在使用这个方法时,注意TextBox控件的ID的命名规则 TextBox1,TextBox2,TextBox3....
和对应的客户端的ID属性。
TextBox的Focus()方法允许程序在服务器端设置文本框的焦点..