比较文本框和预定义列表的值?

时间:2018-06-14 09:43:58

标签: c# asp.net webforms

我是编码的新手。 我已预先定义了一个包含用户名和密码的字符串。我想写逻辑首先找到用户名,然后比较相应文本框中的密码。 如果用户名和密码匹配,我想用另外的代码渲染另一个文件。谢谢。我根据评论编辑了代码,但仍然没有运气。当用户和密码正确时,它会指向else语句。

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridView
{
public partial class practise : System.Web.UI.Page
{


    public static List<User> listuser = new List<User>()
   {
            new User() {ID = 1, UserName = "Dhruv", Password = "hello"},
            new User() {ID = 2, UserName = "Gaurav", Password = "12345"},
            new User() {ID = 3, UserName = "Rahul", Password = "asdfg"},
            new User() {ID = 4, UserName = "Guru", Password = "qwerty"}
   };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listuser = new List<User>();
            GRIDDATA.DataSource = listuser;
            GRIDDATA.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        User data = new User();
        data.UserName = TextBox1.Text;
        data.Password = TextBox2.Text;

        bool UserExists(string UserName, string Password)
        {
            return listuser.Any(a => a.UserName.Equals(UserName) && a.Password.Equals(Password));
        }

        if(UserExists(data.UserName, data.Password))
        {

            Response.Redirect("https://www.google.com");
        }
        else
        {
            Response.Redirect("https://www.wwe.com");
        }





    }
}


public class User
{   
    public int ID { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }   
}
}

2 个答案:

答案 0 :(得分:2)

忽略代码中一些(咳咳)更有趣的区域并阅读您想要的面值问题:

import codecs
import csv
import sys

def read_csv_file(self, file_path):
    is_file = False
    while not is_file:
        if os.path.exists(file_path):
            is_file = True
    result_data = []

    csv.field_size_limit(sys.maxsize)

    csv_reader = csv.reader(codecs.open(file_path, 'rU', 'utf-8'), delimiter=',')

    for row in csv_reader:
        result_data.append(row)

    return result_data

我使用object_instance_list.append(My_Object.objects.get_or_create(property=csv_property[some_index], etc etc)[0]) My_Object.bulk_create(object_instance_list) 使用户名比较不区分大小写。

如果您添加到 public static List<User> listuser = new List<User>(){ new User() {ID = 1, UserName = "Dhruv", Password = "hello"}, new User() {ID = 2, UserName = "Gaurav", Password = "12345"}, new User() {ID = 3, UserName = "Rahul", Password = "asdfg"}, new User() {ID = 4, UserName = "Guru", Password = "qwerty"} }; protected bool UserExists(string userName, string password) { return listuser.Any(a => a.UserName.Equals(userName, StringComparison.CurrentCultureIgnoreCase) && a.Password.Equals(password)); } .Equals(userName, StringComparison.CurrentCultureIgnoreCase)),您真的需要了解您的代码,目前还不是thread safe and will crash...

答案 1 :(得分:0)

让我向您介绍System.Linq;

它使得在泛型集合中的搜索变得容易。

你可以做什么而不是你的foreach(字符串)点是什么。

var loggedInUser = listuser.FirstOrDefault(
    u => u.UserName.Equals(data.UserName) && 
    u.Password.Equals(data.Password)
);

当然要开始工作,你必须添加using System.Linq;位于文档顶部。

如果loggedInUser不是null,那么您就匹配了!