如何在Dropdownlist上进行验证 [英] how to give validation on Dropdownlist

查看:106
本文介绍了如何在Dropdownlist上进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从第一个下拉列表中选择值后,我有6个下拉列表具有相同的值,之后我从dropdownlist2中选择相同的值,以便时间应该给出错误它已经选择了请选择其他选项(其动态下拉列表不是静态的)

I have 6 dropdownlist with same value when i select value from first dropdownlist after that i select same value from dropdownlist2 so that time should be give an error Its already selected please select other option(Its Dynamic Dropdown not static)

推荐答案

使用 CompareValidator [ ^ ]:

Use a CompareValidator[^]:
<asp:CompareValidator runat="server"
    ControlToValidate="SecondDropDownList"
    ControlToCompare="FirstDropDownList"
    Operator="NotEqual"
    Display="Dynamic"
    Text="That value has already been selected - please chose another."
/>



对于六个列表,您需要总共15个验证器以确保没有两个列表相同的值:

  • 比较清单2到清单1;
  • 比较清单3到清单1;
  • 比较清单3到清单2;
  • 比较清单4到清单1;
  • 比较清单4到清单2;
  • 比较清单4到清单3;
  • 比较列表5到列表1;
  • 比较列表5到列表2;
  • 比较列表5到列表3;
  • 比较列表5到列表4 ;
  • 将列表6与列表1进行比较;
  • 将列表6与列表2进行比较;
  • 将列表6与列表3进行比较;
  • 比较清单6到清单4;
  • 比较清单6到清单5;

  • For six lists, you'll need a total of 15 validators to ensure that no two lists have the same value:

    • Compare list 2 to list 1;
    • Compare list 3 to list 1;
    • Compare list 3 to list 2;
    • Compare list 4 to list 1;
    • Compare list 4 to list 2;
    • Compare list 4 to list 3;
    • Compare list 5 to list 1;
    • Compare list 5 to list 2;
    • Compare list 5 to list 3;
    • Compare list 5 to list 4;
    • Compare list 6 to list 1;
    • Compare list 6 to list 2;
    • Compare list 6 to list 3;
    • Compare list 6 to list 4;
    • Compare list 6 to list 5;

    • - 你可以拿临时列表中的所有下拉列表。

      - 对于所有下拉列表的selectedIndexChanged事件,您可以调用某个方法,该方法将使用临时列表ID检查/比较其他下拉列表中的值。



      希望这会给出一些基本的想法...
      - You may take all dropdown's ids in temp list.
      - and for all dropdown's selectedIndexChanged event, you can call some method, which will check/ compare values from other dropdowns using temp list ids.

      hope this would give some basic idea...


      使用此方法

      ========= ================

      Use This method
      =========================
      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      using System.Data.SqlClient;
      
      public partial class Default6 : System.Web.UI.Page
      {
          SqlConnection con = new SqlConnection("Data Source=TMINDZ-18\\SQLEXPRESS;Initial Catalog=adharcard;Persist Security Info=True;User ID=sa;Password=sql");
      
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  fill_drop1();
                  fill_drop2();
                  fill_drop3();
              }
          }
          private void fill_drop1()
          {
              SqlCommand cmd = new SqlCommand("select id,username from login",con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
              DropDownList1.DataSource = ds;
              DropDownList1.DataTextField = "username";
              DropDownList1.DataValueField = "id";
              DropDownList1.DataBind();
         }
          private void fill_drop2()
          {
              SqlCommand cmd = new SqlCommand("select id,username from login", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
              DropDownList2.DataSource = ds;
              DropDownList2.DataTextField = "username";
              DropDownList2.DataValueField = "id";
              DropDownList2.DataBind();
          }
          private void fill_drop3()
          {
              SqlCommand cmd = new SqlCommand("select id,username from login", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
              DropDownList3.DataSource = ds;
              DropDownList3.DataTextField = "username";
              DropDownList3.DataValueField = "id";
              DropDownList3.DataBind();
          }
          
      }



      尝试这种方法....任何疑问告诉我......


      Try This Method....any doubt Tell me...

      这篇关于如何在Dropdownlist上进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆