I'm working on a project in ASP.net (C#) and I have some trouble in concern with the asp:Panel.
Basically, I have an asp:UpdatePanel in which I have a asp:Panel; I Also have asp:TextBox and a button. What I want to do is that : when the user enters text in the asp:TextBox, and then hit the button, the text in the TextBox should Appear in the asp:Panel as a Linkbutton. The Method looks like this :
protected void AddExchange_Click(object sender, EventArgs e)
{
LinkButton link2 = new LinkButton();
link2.Text = AddAdditionalTxt.Text;
// link2.Command += new CommandEventHandler(LinkButton_Command);
ExchangePanel.Controls.Add(link2);
}
It works, and it adds the text as a linkButton to the panel, BUT, when i want to add more, the linkButton which has been added before get overwritten. I want to hold some LIST<string>
Variable that'll hold all the strings, But whenever the page PostBack
it gets deleted.
I would really appreciate if someone could tell me how to solve it - How do i keep a variable (List<>) in a page with UpdatePanel
so every PageLoad
I can add all the strings in the list inside the asp:Panel.
Thanks in advance.
this code works for me, the ViewState
will keep the List<string>
the time the user stays in the page
protected void AddExchange_Click(object sender, EventArgs e)
{
List<string> stringList;
if (ViewState["stringList"] == null)
stringList = new List<string>();
else
stringList = ViewState["stringList"] as List<string>;
stringList.Add(AddAdditionalTxt.Text);
foreach (string myStr in stringList)
{
LinkButton link2 = new LinkButton();
link2.Text = myStr;
// link2.Command += new CommandEventHandler(LinkButton_Command);
ExchangePanel.Controls.Add(link2);
}
ViewState["stringList"] = stringList;
}