Stacie
asked on
Passing variable in txt box..
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 = "";
}
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));
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Not sure why the selected answer has been chosen. That condition will always be true based on what I mentioned above.