aspx页面中包含多个用户控件(.ascx),控件中又有多个文本框、按钮,当在文本框中输入,敲击键盘回车时,会出现异常!解决办法是当点击文本框时执行要触发的按钮,摘抄部分代码如下:
ascx代码:
<table width="80%" style="border:1px solid #DFDFDF" align="center" cellpadding="0" cellspacing="0" bgcolor="#F5F5F5">
<tr>
<td height="26" colspan="2" align="center" bgcolor="#DFDFDF" class="homeTitle"></td>
</tr>
<tr>
<td width="35%" height="42" align="right" class="homeTitle">Password: </td>
<td width="65%" align="left"> <asp:TextBox runat="server" ID="tbPwd" CssClass="input2" Width="139px" Height="13px" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td height="42" colspan="2" align="center">
<asp:Button runat="server" ID="btnSubmit" Text="Submit" CssClass="input2" Width="58px" Height="23px" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
<tr>
<td height="26" colspan="2" align="center" bgcolor="#DFDFDF" class="homeTitle"></td>
</tr>
<tr>
<td width="35%" height="42" align="right" class="homeTitle">Password: </td>
<td width="65%" align="left"> <asp:TextBox runat="server" ID="tbPwd" CssClass="input2" Width="139px" Height="13px" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td height="42" colspan="2" align="center">
<asp:Button runat="server" ID="btnSubmit" Text="Submit" CssClass="input2" Width="58px" Height="23px" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
ascx.cs代码:
protected void Page_Load(object sender, EventArgs e)
{
this.tbPwd.Attributes["onkeypress"] = "if(event.keyCode==13){" + this.btnSubmit.ClientID + ".click();return false;}";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//代码
}
{
this.tbPwd.Attributes["onkeypress"] = "if(event.keyCode==13){" + this.btnSubmit.ClientID + ".click();return false;}";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//代码
}