ASP.NET验证控件 CustomValidator 在使用母版的页面中无法在前台取得控件ID,
而同样的代码在没有母版的页面就能顺利执行,验证控件发挥效用。
这是输入密码框和验证控件的前台代码:
<table style="width: 355px;">
<tr>
<td colspan="2">欢迎登录系统</td>
</tr>
<tr>
<td class="table_rightalign">用户名:</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="150px"></asp:TextBox></td>
</tr>
<tr>
<td class="table_rightalign">密码:</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Width="150px"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="密码长度不能少于6位。" ControlToValidate="txtPassword" ClientValidationFunction="checkpss"></asp:CustomValidator></td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="登录" BackColor="White" BorderColor="#CCCCCC" BorderStyle="Solid" ForeColor="#003300"
Width="50px" OnClick="btnLogin_Click" /></td>
</tr>
</table>
<script type="text/javascript">
function checkpss(source, args) {
var o = document.getElementById("txtPassword");
if (o.value.length < 6)
args.IsValid = false;
else
args.IsValid = true;
}
</script>
运行后报错提示o.value是空引用,也就是var o = document.getElementById("txtPassword");没有获取到前台密码框控件,我换成HTML Input控件用name、ID值获取都没有成功。
很郁闷,求专家解答。