Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net列表框验证通过,但未选择值_C#_Asp.net_Validation_Listbox - Fatal编程技术网

C# asp.net列表框验证通过,但未选择值

C# asp.net列表框验证通过,但未选择值,c#,asp.net,validation,listbox,C#,Asp.net,Validation,Listbox,我有一个列表框,其中包含来自数据库的数据。但即使未选择值,验证也会通过。 我已经试过了,只是简单的必填字段验证器,但也不起作用。 这是代码 <asp:Panel ID="panelDelivery" runat="server" Visible="false" style="position: relative; top: -130px; background-color: #66FF33; left: 0px;" >

我有一个列表框,其中包含来自数据库的数据。但即使未选择值,验证也会通过。 我已经试过了,只是简单的必填字段验证器,但也不起作用。 这是代码

           <asp:Panel ID="panelDelivery" runat="server"  Visible="false" 
               style="position: relative; top: -130px; background-color: #66FF33; left: 0px;"  >
           <asp:Label ID="lblOverWeight" runat="server" Width="344px"></asp:Label><br />
           <asp:ListBox ID="listBxDelivery" runat="server" DataSourceID="Delivery" 
               DataTextField="DataText" DataValueField="Price" Width="489px"         
               AppendDataBoundItems="True" CausesValidation="True" AutoPostBack="True" 
                   style="top: 0px; left: 0px">
           </asp:ListBox>

默认情况下,requiredFieldValidator将比较其初始值default is和ControlToValidate所选值。确保您没有设置listbox selectedvalue,如果您更改它,也会更改requiredFieldValidator的初始值。 见下面的例子

<asp:ListBox ID="ListBox1" runat="server">
                            <asp:ListItem Text="1" Value="1" ></asp:ListItem>
                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>
                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>
    </asp:ListBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"    ErrorMessage="Error, Select value" ControlToValidate="ListBox1"></asp:RequiredFieldValidator>


RequiredFieldValidator仅在控件中没有数据时工作。因为您正在谈论从列表框中选择,所以您可能需要一个customValidator,然后在那里执行检查。
<asp:ListBox ID="ListBox1" runat="server">
                            <asp:ListItem Text="1" Value="1" ></asp:ListItem>
                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>
                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>
    </asp:ListBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"    ErrorMessage="Error, Select value" ControlToValidate="ListBox1"></asp:RequiredFieldValidator>
<asp:ListBox ID="ListBox1" runat="server">
                        <asp:ListItem Text="1" Value="1" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="2" Value="2" ></asp:ListItem>
                        <asp:ListItem Text="2" Value="2" ></asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ListBox1" InitialValue="1"></asp:RequiredFieldValidator>