使用Java脚本基于另一个控件项来验证ASP.NET C#控件 - c#

我正在尝试确保一个下拉框或另一个下拉框具有选定的条目,不能两者都选,并且不能都留空/未选择。每次单击运行javascript验证代码的搜索按钮时,即使我只是从一个下拉列表中选择,甚至从两个下拉列表中都没有选择,我都会收到一条消息,好像在两个下拉框中都选择了一样!这可能是我的逻辑,但是,我认为也可能是我的变量读为null。是否有人对问题可能是什么以及如何解决这个问题有任何想法?

Javascript:
函数userValid(){

var Season = document.getElementById('<%=cboMonth.ClientID %>');
var Month = document.getElementById('<%=lboChosenSeason.ClientID %>');
var Day = document.getElementById('<%=cboDay.ClientID %>');

if (Season == 'none' && Month == 0) {
    alert("A Season OR a Month is required.");
    return false;
}
if (Season != 'none' && Month != 0) {
    alert("Please select either a season OR a month.");
    return false;
}
else {
    return true;
}

}

ASP.NET

<%@ Page Language="C#"  Title="Search for a class by date" 
  MasterPageFile="~/Site.master" 
  AutoEventWireup="true" 
  CodeBehind="SearchByDate.aspx.cs" 
  Inherits="ClassesnWorkshops.SearchByDate" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
        <script src="UserValidation.js" type="text/javascript"></script>         
    </asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <div align="center" style="background-color: transparent; background-image: url(Images/panel-SearchByDate.png); height: 265px; background-repeat:no-repeat; background-position: 50% 50%;">
   <div style="height: 100%; padding-left: 1%"> 
           <br />
           &nbsp; &nbsp;
            <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="Images/hotspot.gif" ForeColor="Black" Height="16px" Width="605px">
                <asp:RectangleHotSpot AlternateText="Search By Keyword" Left="0" Top="15" Right="75" Bottom="0" NavigateUrl="~/Default.aspx" />
                <asp:RectangleHotSpot AlternateText="Search By Age" Left="100" Top="20" Right="185" Bottom="0" NavigateUrl="~/SearchByAge.aspx" />
                <asp:RectangleHotSpot AlternateText="Search By Price" Left="200" Top="25" Right="290" Bottom="0" NavigateUrl="~/SearchByPrice.aspx" />
                <asp:RectangleHotSpot AlternateText="Search By Date" Left="310" Top="30" Right="395" Bottom="0" NavigateUrl="~/SearchByDate.aspx" />
                <asp:RectangleHotSpot AlternateText="Search By Class Type" Left="410" Top="35" Right="505" Bottom="0" NavigateUrl="~/SearchByType.aspx" />
                <asp:RectangleHotSpot AlternateText="Advanced Search" Left="520" Top="25" Right="605" Bottom="0" NavigateUrl="~/AdvSearch.aspx" />
             </asp:ImageMap>
           <br />
           <br />
           <asp:Label ID="lblDate" runat="server" Text="Choose a year and season or month for the start of your class: " ForeColor="Black"></asp:Label>
           <br /><br />
           <asp:Label ID="lblYear" runat="server" Text="Year: " ForeColor="Black"></asp:Label>
           <asp:DropDownList ID="cboYear" runat="server" width="60px" tooltip="Choose a year for classes.">
           </asp:DropDownList> &nbsp; &nbsp; &nbsp;
           <asp:Label ID="lblSeason" runat="server" Text="Season: " ForeColor="Black"></asp:Label>     

           <asp:ListBox ID="lboSeason" runat="server" Rows="1" class="cSeason"
               onselectedindexchanged="lboSeason_SelectedIndexChanged">
               <asp:ListItem>none</asp:ListItem>
               <asp:ListItem>Spring 1</asp:ListItem>
               <asp:ListItem>Spring 2</asp:ListItem>
               <asp:ListItem>Summer 1</asp:ListItem>
               <asp:ListItem>Summer 2</asp:ListItem>
               <asp:ListItem>Fall 1</asp:ListItem>
               <asp:ListItem>Fall 2</asp:ListItem>
           </asp:ListBox>

           &nbsp; &nbsp; &nbsp; &nbsp; 
<asp:Label ID="lblOR" runat="server" Text=" OR " ForeColor="Black"></asp:Label>
            &nbsp; 
           <asp:Label ID="lblMonth" runat="server" Text="Month: " ForeColor="Black"></asp:Label>

           <asp:ListBox ID="cboMonth" runat="server" Rows="1">
               <asp:ListItem>0</asp:ListItem>
               <asp:ListItem>1</asp:ListItem>
               <asp:ListItem>2</asp:ListItem>
               <asp:ListItem>3</asp:ListItem>
               <asp:ListItem>4</asp:ListItem>
               <asp:ListItem>5</asp:ListItem>
               <asp:ListItem>6</asp:ListItem>
               <asp:ListItem>7</asp:ListItem>
               <asp:ListItem>8</asp:ListItem>
               <asp:ListItem>9</asp:ListItem>
               <asp:ListItem>10</asp:ListItem>
               <asp:ListItem>11</asp:ListItem>
               <asp:ListItem>12</asp:ListItem>
           </asp:ListBox>

           &nbsp; Optional Day:
           <asp:DropDownList ID="cboDay" runat="server" width="50px" tooltip="Choose minimum cost of class or group of classes.">
           </asp:DropDownList>
           <br />
           &nbsp; &nbsp; &nbsp;           
           <br />    
            <asp:Label ID="lblSearch" runat="server" Text="Search: " ForeColor="Black"></asp:Label>
           <asp:TextBox ID="txtSearchWords" runat="server" Width="250px" tooltip="Place a keyword such as subject, category or title."></asp:TextBox>
           <br />
           <br />
<asp:Label ID="lblZipCode" runat="server" Text="Zipcode: " ForeColor="Black"></asp:Label>
            <asp:TextBox ID="txtZipCode" runat="server" Width="80px" tooltip="Type your 5 digit zipcode as a center point for a distance search."></asp:TextBox>
                &nbsp;&nbsp;&nbsp; 
            <asp:Label ID="lblDistance" runat="server" Text="Distance: " ForeColor="Black"></asp:Label>           
            <asp:DropDownList ID="cboWithin" runat="server" width="100px" tooltip="Choose the miles your are willing to travel to your class.">               
            </asp:DropDownList>
            <br />
            <br />
            <asp:Button ID="cmdSearch" runat="server" Text="Search" onclick="cmdSearch_Click" OnClientClick="return userValid();" />                
                &nbsp; &nbsp; 
<asp:Label ID="lblChosenSeason" runat="server" Text="Label"></asp:Label>
                <br />

参考方案

您正在使用元素变量作为它们的值。您需要使用这些值。

if (Season.value == 'none' && Month.selectedIndex == 0) {
    alert("A Season OR a Month is required.");
    return false;
}

相对于:

if (Season == 'none' && Month == 0) {
    alert("A Season OR a Month is required.");
    return false;
}

ASP.net C#崩溃一行 - c#

我有一个母版页,在on load事件中包含以下几行: string menuIDdata = Page.Request.QueryString["mid"]; menuID = 0; // Get the menu ID if (!int.TryParse(menuIDdata, out menuID)) { menuID = 0; } …

Asp.net发送信息表单到页面 - c#

我正在尝试使用弹出窗口中的新信息更新旧页面。到目前为止,我尝试过将结果保存在会话中Session["Data"] = DLvrijecampingplaatsen.SelectedItem; 然后当它达到Page_Load时,将其重新加载回旧页面if (Session["Data"] != null) { LBkies…

与Mootools Scriptmanager Ajax Asp.net发生冲突? - c#

我正在尝试不同的方法来使这项工作成功,但是没有成功。我正在尝试将mootools与asp.net应用程序集成。我只想用它为我的网站添加一些效果。我也使用更新面板,scriptmanager,因为不希望有完整的回发。在控制台上引发错误TypeError:clientID.startsWith不是函数,并且在进行更新时brwoser reset...。这是整个代…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

将asp.net.core 2.0应用发布到IIS时JavaScript无法运行 - javascript

我是这里的新手。我有一个VS2017 asp.net C#Web应用程序。我目前不在使用Angular。这是一个简单的网站。我隐藏了div,当用户单击菜单项时,相关的div出现,而我隐藏了其余的div。我使用JavaScript完成此任务。在开发和IIS Express中,它可以工作。当我发布到IIS时,它不起作用。静态页面显示“确定”,但菜单按钮不执行任何…