Javascript无法在母版页中使用

时间:2014-10-20 14:04:56

标签: javascript asp.net .net master-pages web-deployment

我有一个使用母版页的登录页面,我通过javascript应用验证,但它无效。

<script type="text/javascript">

    function checkField(form)
    {
        var id = document.getElementById('<% =txtLoginId.ClientID %>').value;
        var pwd = document.getElementById('<% =txtPassword.ClientID %>').value;
        alert("testing" + id);
        if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
            lblUserId.visible = true;
            form.txtLoginId.focus();
            return false;
        }
        if ((pwd == '' && id != '') || (pwd == null && id != null)) {
            lblPwd.visible = true;;
            form.txtPassword.focus();
            return false;
        }
        if ((id == '' && pwd == '') || (id == null && pwd == null)) {
            lblBoth.visible = true;
            form.txtLoginId.focus();
            return false;
        }
    }
</script>

如果我在用户名中给出了一些价值,它会在警报中显示我,但如果我没有给出任何值,那么if条件应该有效但是它不起作用,警报只是显示&#34;测试& #34 ;. 这是我的HTML代码。

<form id="form" method="post" action="Login.aspx">
      <div style="left:37%;position:absolute;top:55%;background-color:red">
        <table style="left:0%; position:absolute;top:0%; width: 265px">
            <asp:Label ID="lblUserId" runat="server" ForeColor="Red" Visible="false"  Text="* Username is Required!"></asp:Label>
            <asp:Label ID="lblPwd" runat="server" ForeColor="Red" Visible="false"  Text="* Passowrd is Required!"></asp:Label>
            <asp:Label ID="lblBoth" runat="server" ForeColor="Red" Visible="false"  Text="* Username and Password are Required!"></asp:Label>
         </table>
      </div>
    <div style="left:37%;position:absolute;top:60%;background-color:red">
        <table style="left:0%; position:absolute;top:0%; width: 265px;border:solid;background-color:darkorange">
            <tr align="center">
                <td align="center">
                    <asp:Label ID="lblHead" runat="server" Font-Bold="true"  Text="Mobile Customer Order Tracking"></asp:Label>
                </td>
            </tr>
         </table>
      </div>
      <div style="left:37%;position:absolute;top:65%">
         <table style="border:solid;">
            <tr>
              <td>
                 <asp:Label ID="lblLoginId" runat="server" Text="Login ID"></asp:Label>
              </td>
              <td>
                 <asp:TextBox ID="txtLoginId" ClientIDMode="Static" runat="server" />
              </td>
            </tr>
            <tr>
               <td>
                  <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
               </td>
               <td>
                  <asp:TextBox ID="txtPassword" TextMode="Password" ClientIDMode="Static" runat="server"></asp:TextBox>
               </td>
            </tr>
            <tr>
               <td>
                  <asp:Label ID ="lblRemember" runat="server" Text="Remember My ID"></asp:Label>
                  <asp:CheckBox ID ="chkRemember" runat="server" />
               </td>
               <td align="right">
                   <asp:Button ID="btnLogin" runat="server" text="Login" OnClientClick="return checkField(this);"/>
               </td>
             </tr>
         </table>
       </div>
   </form>

2 个答案:

答案 0 :(得分:1)

你检查过javascript例外吗?

您正在通过服务器端ID引用asp元素,前端无法找到它:

    if ((id == '' && pwd != '' )|| (id == null && pwd != null)) {
        lblUserId.visible = true; //here
        form.txtLoginId.focus();
        return false;
    }
    if ((pwd == '' && id != '') || (pwd == null && id != null)) {
        lblPwd.visible = true; //here
        form.txtPassword.focus(); 
        return false;
    }
    if ((id == '' && pwd == '') || (id == null && pwd == null)) {
        lblBoth.visible = true; //here
        form.txtLoginId.focus();
        return false;
    }

<强>解决方案:

可以解决这个问题,为这些用户控件设置ClientIDMode = "static",或者像在其他地方一样在javascript中使用<%= lblUserId.ClientID %>

答案 1 :(得分:0)

您应该使用必需的字段验证器,节省时间,高效,客户端,并且不需要太多努力。