在逗号分隔的文本框中重复值的问题 [英] Issue of repeating values in textbox separated by comma

查看:65
本文介绍了在逗号分隔的文本框中重复值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,

我有一个网格视图,其中包含带有文本框的模板字段。我的问题是当我写一个带有重复值的字符串,比如ab,ab,cf,hj这样的话就应该显示一条消息。

我已经使用了自定义验证器,但它没有用。

我的代码是

Hello Everyone,
I have a grid view in which a template field with text boxes are present. My issue is when i write a string with repeated values like ab,ab,cf,hj like this then a message should show.
I have used a custom validator for it,but it is not working.
My code is

protected void CustomValidator5_ServerValidate(object source, ServerValidateEventArgs args)
 {

     CustomValidator cv = (CustomValidator)source;
     GridViewRow gvr = (GridViewRow)cv.NamingContainer;
     CheckBox chk = (CheckBox)gvr.FindControl("CheckBox1");
     TextBox txt1 = (TextBox)gvr.FindControl("TextBox2");
     TextBox txt2 = (TextBox)gvr.FindControl("TextBox6");
     TextBox txt3 = (TextBox)gvr.FindControl("TextBox3");
     Label lab = (Label)gvr.FindControl("Label1");
     string serials = txt2.Text;
     string[] sl = serials.Split(',');
     if (chk.Checked == true)
     {
         if (txt1.Text != "")
         {
             using (SqlCommand comm = new SqlCommand("select [Serial_Number_Required] from Products where Product=@Product ", con))
             {
                 con.Open();
                 //string Role = (string)(Session["Role"]);
                 // 2. define parameters used in command object
                 SqlParameter para = null;
                 para = new SqlParameter();
                 para.ParameterName = "@Product";
                 para.Value = lab.Text;
                 comm.Parameters.Add(para);

                 //Pass @Pages parameter

                 SqlDataReader reade = comm.ExecuteReader();
                 while (reade.Read())
                 {
                     Session["Serial_Number_Required"] = Convert.ToString(reade["Serial_Number_Required"]);
                 }
                 con.Close();
                 //Button Add = (Button)PreviousPage.FindControl("Button2");
                 if (Session["Serial_Number_Required"].ToString() == "Y")
                 {
                     for (int i = 0; i < sl.Length; i++)
                     {
                         for (int j = i + 1; j < sl.Length; j++)
                         {
                             if (sl[i].ToString() == sl[j].ToString())
                             {
                                 args.IsValid = false;
                             }
                             else
                             {
                                 args.IsValid = true;
                             }
                         }
                     }

                 }
             }

         }

     }
 }




<asp:CustomValidator ID="CustomValidator5" runat="server" ErrorMessage="Enter unique serialNo."

OnServerValidate="CustomValidator5_ServerValidate" ValidateEmptyText="true"

ControlToValidate="TextBox2" Display="Dynamic"

style="font-size: x-small; font-family: Verdana" ValidationGroup="txt"

Font-Bold="True" ForeColor="Red"></asp:CustomValidator>

推荐答案

for (int i = 0; i < sl.Length; i++)
                        {
                            for (int j = i + 1; j < sl.Length; j++)
                            {
                                if (sl[i].ToString() == sl[j].ToString())
                                {
                                    args.IsValid = false;
//you should break the loop here
break;
                                }
                                else
                                {
                                    args.IsValid = true;
                                }
                            }
//if already match found break from here as well
if(args.IsValid == false) break;

                        }


如果您使用的是.net 3.5及以上版本:



If you're using .net 3.5 and above:

sl = (string[])sl.Distinct().ToArray(typeof(string));





这种方式你只处理不同的值



如果警告用户很重要,那么你创建新的数组并比较sl和这个新数组的长度。





This way you're only processing distinct values

If it is important to warn the user, then you create new Array and compare length of sl and this new array.

String[] sl_distinct = (string[])sl.Distinct().ToArray(typeof(string));
if (sl.length != sl_distinct.length){
// your message
}


这篇关于在逗号分隔的文本框中重复值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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