2 つのフィールドがあり、もう一方のフィールドに「NO」値のコンテンツが含まれている場合にのみ、フィールドの 1 つを必須フィールドにする必要があります。そこでネットで調べた結果、これにたどり着きました。私が持っていて、これについて助けが必要な唯一の質問は、 this: を呼び出すコードの最初の部分です.getElementByID
。どうすればそれを構築できますか?または、いくつかのライトを当てることができれば素晴らしいでしょう。
<script>
function Validate(source, args) {
if (document.getElementById('<%= TextBox1.ClientID %>').value.toUpperCase() == "NO" &&
document.getElementById('<%= TextBox2.ClientID %>').value.length == 0)
args.IsValid = false;
else
args.IsValid = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TextBox2 Should Be Filled"
Display="Dynamic" ClientValidationFunction="Validate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Page.Validate();
if (!Page.IsValid)
Response.Write("TextBox2 should be filled");
}
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = !(this.TextBox1.Text.Equals("NO", StringComparison.CurrentCultureIgnoreCase) && this.TextBox2.Text.Length == 0);