【问题标题】:Have Validation with DropDownList使用 DropDownList 进行验证
【发布时间】:2016-06-30 15:41:05
【问题描述】:

我有一个 DropDownList,它绑定到页面的 PageLoad 上的 DataSource。在这个下拉列表中,我添加了一个 Select 值作为默认值。在这个下拉列表和一些文本框的底部,我有一个添加按钮。如何在下拉列表中添加验证,以便如果用户将值保留为“选择”,他/她无法单击添加按钮,除非他/她选择另一个值。

    <asp:Label ID="lblItems" runat="server" Text="SemesterCode: "></asp:Label>
<asp:DropDownList ID="ddlItems" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text=" -- Select -- " Value="-1"></asp:ListItem>
</asp:DropDownList>

【问题讨论】:

    标签: c# asp.net validation drop-down-menu


    【解决方案1】:

    对下拉列表使用“onchange”方法。当 DDL 的值发生变化时,调用一个 JS 函数,该函数将启用其他按钮/必填字段。

    function validateThingy(val) {
      return (val != -1)
    }
    
    function calledOnChange(val) {
      if (validateThingy(val)) {
        //enable the buttons
      } else {
        //Keep the button disabled
      }
    }    
    

    【讨论】:

      【解决方案2】:

      检查您的下拉列表的选定索引如果它是 0 则警告一条消息,否则同样放置您的逻辑

      if(ddlItems.SelectedIndex==0){
      Response.Write("Please choose any value");
      }else{
       //Put your logic here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-13
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 2012-06-23
        • 2014-03-18
        • 2012-01-21
        相关资源
        最近更新 更多