I have a .aspx application where the user inputs a name and that name is added to a list. This can be done up to five times. When the user clicks the button, the first name entered will display in the first label. When the user inputs another name and clicks the button the first label remains the same and the next label displays the new name and so on. My problem is the list is reset on PostBack. I am trying to use ViewState to help solve this with no success. Any help is greatly appreciated.
Edit: I got it working so thank you everybody for your help. There is still a lot of room for improvement but this is a great starting point.
[Serializable]
class Recipient
{
public string Fname { get; set; }
public string MInit { get; set; }
public string Lname { get; set; }
public string Suffix { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEnter_Click(object sender, EventArgs e)
{
Recipient recipients = new Recipient();
List<string> FName = (List<string>)ViewState["recipientList"];
List<string> MInit = (List<string>)ViewState["recipientList"];
List<string> LName = (List<string>)ViewState["recipientList"];
if (FName == null && MInit == null && LName == null)
{
FName = new List<string>();
MInit = new List<string>();
LName = new List<string>();
}
recipients.Fname = txtFName.Text;
recipients.MInit = txtMinit.Text;
recipients.Lname = txtLName.Text;
FName.Add(recipients.Fname);
MInit.Add(recipients.MInit);
LName.Add(recipients.Lname);
ViewState["recipientList"] = FName;
ViewState["recipientList"] = MInit;
ViewState["recipientList"] = LName;
if (FName.Count == 1 && MInit.Count == 1 && LName.Count == 1)
{
lblFName.Text = FName[0] + " " + MInit[0] + " " + LName[0];
}
if (FName.Count == 4 && MInit.Count == 4 && LName.Count == 4)
{
lblFName1.Text = FName[1] + " " + MInit[2] + " " + LName[3];
}
}
I'm not sure the purpose of Recipient class. Anyhow, you want to instantiate Recipient list before adding a recipient.
<asp:TextBox runat="server" ID="txtFName" /><br />
<asp:Button runat="server" ID="btnEnter" Text="Submit" OnClick="btnEnter_Click" /><br />
<asp:Label runat="server" ID="lblFName" /><br />
<asp:Label runat="server" ID="lblFName1" /><br />
<asp:Label runat="server" ID="lblFName2" /><br />
<asp:Label runat="server" ID="lblFName3" /><br />
<asp:Label runat="server" ID="lblFName4" /><br />
[Serializable]
public class Recipient
{
public string name { get; set; }
}
public List<Recipient> recipientList
{
get
{
if (ViewState["recipientList"] != null)
return (List<Recipient>)ViewState["recipientList"];
return new List<Recipient>();
}
set { ViewState["recipientList"] = value; }
}
protected void btnEnter_Click(object sender, EventArgs e)
{
List<Recipient> recipient = recipientList;
recipient.Add(new Recipient{ name = txtFName.Text.Trim()});
recipientList = recipient;
int count = recipient.Count;
if (count == 1)
lblFName.Text = recipientList[0].name;
if (count > 1)
lblFName1.Text = recipientList[1].name;
if (count > 2)
lblFName2.Text = recipientList[2].name;
if (count > 3)
lblFName3.Text = recipientList[3].name;
if (count > 4)
lblFName4.Text = recipientList[4].name;
}