?
Solved

Passing variable in txt box..

Posted on 2014-09-22
3
Medium Priority
?
168 Views
Last Modified: 2014-09-30
Not sure what is wrong with this code from my .aspx page... Not getting any result when I click on the button.

 protected void Button1_Click(object sender, EventArgs e)
    {
        List<Person> People = new List<Person>();


        // private List<Person> people = new List<Person>();

        Person person = new Person();
        {
            person.FirstName = TextBox1.Text;
            person.LastName = TextBox2.Text;            

        };
        if (People.Contains(person))
        {

            Label1.Text = "The list does not contains this person";

        }
        else {
            People.Add(person);
            TextBox1.Text = "";
            TextBox2.Text = "";
        
        }

Open in new window




public class Person : IEquatable<Person>
{
	
		public string LastName { get; set;}
        public string FirstName { get; set;}


       public bool Equals(Person other){
       
        return ((FirstName == other.FirstName) && (LastName == other.LastName));
       
       
       }

}

Open in new window

0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 40337465
Other than it always says that the user exists even if it doesn't change your code to:
               if (!(People.Contains(person)))
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40338927
Your list is local to the click handler.  You are recreating the list every time you click the button, so it is always empty. You need to widen the scope of the list variable--i.e. make it a class-level variable instead of a method-level variable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40353138
Not sure why the selected answer has been chosen. That condition will always be true based on what I mentioned above.
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question