【问题标题】:asp.net Validation on DropDownList box下拉列表框上的 asp.net 验证
【发布时间】:2011-02-25 10:04:26
【问题描述】:

我有一个显示专辑值的下拉列表 (cboViewAlbums)。第一项是 请选择专辑...。我正在尝试使用验证,如果 cboViewAlbums 列表选择了值 0,则单击 lb_create_album linkBut​​ton 会引发错误。 下面是这个和我的尝试的代码:

<asp:DropDownList ID="cboViewAlbums" runat="server" 
         DataSourceID="SqlDataSource1" DataTextField="album_name" 
         DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
         <asp:ListItem Value="0">Please select an album...</asp:ListItem>
         </asp:DropDownList>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
        SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
    </asp:SqlDataSource>
     <br />
     <asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
     <br />
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
         ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album" 
         InitialValue="Please select an album..."></asp:RequiredFieldValidator>

知道我怎样才能让它工作吗?

谢谢

【问题讨论】:

    标签: asp.net validation drop-down-menu asp.net-mvc-validation


    【解决方案1】:

    您必须使用带有下拉列表的范围验证器并设置大于 0 的最小值和最大值来设置任何最大值,同时提供最小值和最大值的类型值,即 整数。 p>

    以下是我为您制作的示例代码,您必须绑定静态列表项的数据源。

    <asp:DropDownList runat="server" ID="ddl1" >
            <asp:ListItem Value="0" Text="Select value" />
            <asp:ListItem Value="1" Text="text1" />
            <asp:ListItem Value="2" Text="text2" />
        </asp:DropDownList>
        <asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
            MinimumValue="1" MaximumValue="100000000" Type=Integer />
        <asp:Button Text="text" runat="server"  />
    

    如果对你有帮助,请标记为答案

    谢谢

    阿伦

    【讨论】:

      【解决方案2】:

      首先,您不能使用 HyperLink 进行验证,最好使用 LinkBut​​ton。 HyperLink 不会回发,所以这是您的第一个错误。 其次,在您的RequiredFieldValidator 上,输入initialvalue=0,这应该可以解决您的问题。

      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
           ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album" 
           InitialValue="0"></asp:RequiredFieldValidator>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        相关资源
        最近更新 更多