简体   繁体   English

会话中的C#列表元素无响应

[英]C# List Elements in the Session Not Responding

I'm doing a simple program to add a student(with ID,Name) to a List, then to search Student by ID through session. 我正在做一个简单的程序,将一个学生(带有ID,名称)添加到列表中,然后通过会话按ID搜索学生。

Add Student Module is like below, 添加学生模块如下所示,

protected void addStudent(object sender, EventArgs e)
        {
            List<Student> thisstdlist = new List<Student>();
            thisstdlist = (List<Student>)Session["stdlist"];
            thisstdlist.Add(new Student(txtsid.Text,txtsname.Text));
            Session["stdlist"] = thisstdlist;
            Response.Redirect("Home.aspx");


        }

Search Student Module is Like Below, 搜索学生模块如下所示,

  protected void searchStudent(object sender, EventArgs e)
        {


            foreach (Student element in (List<Student>)Session["stdlist"])
            {
               if(element.getID().Equals(txtstdid.Text)){
                   txtstdname.Text = element.getName();
               }
           }
        }

Student Class is like below, 学生班如下

public class Student
    {
        private String Name;
        private String ID;

        public Student(String sid, String sn) {

            this.Name = sn;
            this.ID = sid;

        }

        public String getName() {

            return this.Name;
        }
        public String getID()
        {

            return this.ID;
        }


    }

But when I added students, for ex: 100,John and Search by 100 it gives me no result. 但是,当我添加学生时,例如:100,约翰和按100进行搜索,则没有结果。 Please can anyone show me the mistake or the correct way of doing this. 请任何人告诉我错误或正确的做法。

are you setting breakpoints and actually checking what the values of these lists and what is actually stored in the session? 您是否设置断点并实际检查这些列表的值以及会话中实际存储的值?

.Equals() is not doing what you think it is .Equals()并没有按照您的想法做

try : 尝试:

 foreach (Student element in (List<Student>)Session["stdlist"])
            {
               if(element.ID == txtstdid.Text){
                   txtstdname.Text = element.getName();
               }
           }

The add Student module won't initialize the student list correctly - you are creating a new List<Student> and then throwing the new list away with the next line assignment. add Student模块将无法正确初始化学生列表-您正在创建新的List<Student> ,然后将新列表与下一行分配一起丢弃。 I would go with something like: 我会喜欢这样的:

var thisstdlist = (List<Student>)Session["stdlist"];
// If a key isn't found in Session, it will be null ..
if (thisstdlist == null)
{
    // i.e. only re-initialize if it was missing from session
    thisstdlist = new List<Student>();
    // You don't need to continually reassign the session variable
    Session["stdlist"] = thisstdlist;
}
// Adds to the list; now that Session also has a reference to the same list
thisstdlist.Add(new Student(txtsid.Text,txtsname.Text));

As per the comment, note that c# has automatic (albeit mutable) properties - you don't need the Java-style getters and setters. 根据注释,请注意c#具有自动(尽管可变)属性-您不需要Java风格的getter和setter。

public class Student
{
    public Student(string sid, string sn) 
    {
        Name = sn;
        ID = sid;
    }

    public string Name 
    {
        get;
        set;
    }
    public string ID
    {
        get;
        set;
    }
}

Also, in .Net, == for strings is overridden to test values (unlike Java's reference equality for strings), so you can rewrite the comparison as: 此外,在.Net中,字符串的== 会覆盖测试值(与Java的字符串参考相等性不同),因此您可以将比较重写为:

if (element.ID == txtstdid.Text)
{
    txtstdname.Text = element.Name;
}

Re : foreach - I guess means that you are using the List in a Dictionary (HashMap) fashion - if you use Dictionary instead of List - this will allow you do remove the foreach in favour of: 回复: foreach -我猜你正在使用的工具ListDictionary (HashMap的)的方式-如果你使用Dictionary而不是List -这将让你删除foreach赞成:

// addStudent ...
var txtstdname = new Dictionary<string, Student>();
 // ... 
txtstdname.Add(txtsid.Text, new Student(txtsid.Text,txtsname.Text))

// searchStudent ...
Student element = null;
if (txtstdname.TryGetValue(out element))
{
    txtstdname.Text = element.Name();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM