c#字符串条件在这里不起作用

时间:2012-07-31 11:17:06

标签: c#

我在if条件下有一些电子邮件帐户并做出一些简单的决定但这不适用于我请帮我找出问题。这是我的代码

 if (!this.IsPostBack)
            {
                if ((lblemail.Text.Equals("ali.saleem@shakarganj.com.pk")) || (lblemail.Text.Equals("altaf.saleem@shakarganj.com.pk")) || (lblemail.Text.Equals("anjum.saleem@shakarganj.com.pk")) || (lblemail.Text.Equals("ibrahim.cheema@shakarganj.com.pk")) || (lblemail.Text.Equals("malik.adeel@shakarganj.com.pk")))
                {
                    GridView2.Visible = true;
                    GridView1.Visible = false;
                }
                else
                {
                    GridView1.Visible = true;
                    GridView2.Visible = false;
                }
            }

在这里,我想以一种简单而恰当的方式来处理我的要求,请帮助我

3 个答案:

答案 0 :(得分:1)

不应该是“tbEmail”(或类似的东西)而不是“lblemail”? lblemail.Text的价值是什么?

答案 1 :(得分:1)

制作所有电子邮件地址的List<string>并执行以下检查:

List<string> emailAddresses = CreateList();
if(emailAddresses.Contains(lblemail.Text))
{
      // Whatever you need to do
}
else
{
      // do the other thing
}

答案 2 :(得分:0)

一切看起来都很正确。也许问题是由于大写或修剪?试试这个:

if ((lblemail.Text.ToLower().Trim().Equals("ali.saleem@shakarganj.com.pk")) || 
    (lblemail.Text.ToLower().Trim().Equals("altaf.saleem@shakarganj.com.pk")) ||    
    (lblemail.Text.ToLower().Trim().Equals("anjum.saleem@shakarganj.com.pk")) || 
    (lblemail.Text.ToLower().Trim().Equals("ibrahim.cheema@shakarganj.com.pk")) || 
    (lblemail.Text.ToLower().Trim().Equals("malik.adeel@shakarganj.com.pk")))
{
    GridView2.Visible = true;
    GridView1.Visible = false;
}