2010-09-17 22 views
0

使用javascript我使用的JavaScript的CustomValidator在ASP.Net驗證文本框驗證文本框。 當我使用普通頁面時,該代碼完美工作,但只要我把它放在MasterPage中,代碼就無法工作。在母版

下面是我的aspx頁面的代碼。如果我把這段代碼放在MasterPage裏面,它不起作用。 你能不能告訴我如何使這項工作在MasterPage內

謝謝, Abhi。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 

<script type="text/javascript"> 
function validateOrFields(source, args){ 
var sUser = document.getElementById('TextBox1'); 

    if (sUser.value == "") 
    { 
     args.IsValid = false; 
    } 
    else 
    { 
     args.IsValid = true; 
    } 
    return; 
} 

<div> 
    <asp:TextBox ID="TextBox1" 
     runat="server"></asp:TextBox> 
     <asp:CustomValidator ID="FieldValidator" 
        runat="server" 
        Text="Enter either a user name" 
        ClientValidationFunction="validateOrFields" onservervalidate="FieldValidator_ServerValidate"/> 
     <asp:Button ID="Button1" runat="server" Text="Button" /> 
</div> 
</form> 
</body> 
</html> 

回答

1

您需要使用ClientID

替換你的代碼通過

var sUser = document.getElementById('<%= TextBox1.ClientID %>'); 
+0

非常感謝,它工作得很好。 – 2010-09-17 09:54:25

2

問題是TextBox的ID。放置在母版頁中時,它將獲得不同的ClientID。

var sUser = document.getElementById('<%= TextBox1.ClientID %>'); 
+0

非常感謝,它工作得很好。 – 2010-09-17 09:54:52

0

嘗試的document.getElementById( 'ct100_TextBox1')值來獲得ID。這只是一個臨時修復!但是任何人都請建議如何使用代碼動態獲取此ID。我通過查看生成的頁面的源代碼來獲得此信息。我也貼了同樣的問題在這裏 link text