I created a List as global variable in my page.
public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();
And in a function called during Page_Load, I add some elements like this:
foreach (var childControl in allControlsLinkButton)
{
if (childControl.CssClass == "linkButtonSalleActive" || childControl.CssClass == "linkButtonSalle")
{
allControlsLinkButtonSalles.Add(childControl);
}
}
Just after that, when I do this:
foreach (LinkButton value in allControlsLinkButtonSalles)
{
literal2.Text += " <br /> Text " + value.Text;
}
And there are definitely 3 elements that show up.
However when I try to do this:
literal2.Text += " First element " + allControlsLinkButtonSalles.First().Text;
An error occurs. How come that is possible ?
Here is the message:
Description : An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The sequence contains no elements.
Source Error:
Ligne 605 : }
Ligne 606 :
Ligne 607 : literal2.Text += " First " + allControlsLinkButtonSalles.First().Text;
Ligne 608 :
Ligne 609 : //allControlsLinkButtonSalles[0].CssClass = "linkButtonSalleActive";
Stack Trace:
[InvalidOperationException: The sequence contains no elements.]
System.Linq.Enumerable.First (IEnumerable `1 source) +269
test2MasterPage.Page_init() in c:\Users....\Documents\Visual Studio 2012\WebSites\test1\test2MasterPage.aspx.cs:607
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +9807957
System.Web.UI.Control.OnInit(EventArgs e) +92
System.Web.UI.Page.OnInit(EventArgs e) +12
System.Web.UI.Control.InitRecursive(Control namingContainer) +134
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +489
Here is the complete code:
public static List<DataTable> ListTable = new data().GetTable();
public static List<string> SallesList = new data().SallesListCreation(ListTable[0]);
//DataTable dt = new data().
public static int Load_Counter = 0;
List<Button> allControlsButton = new List<Button>();
public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();
List<LinkButton> allControlsLinkButtonAffichages = new List<LinkButton>();
List<LinkButton> allControlsLinkButtonSemaine = new List<LinkButton>();
protected void Page_Load(object sender, EventArgs e)
{
literal2.Text += "<br /> counter : " + Load_Counter.ToString();
DateTime today = DateTime.Now;
string sToday = DateTime.Now.ToString("dd/MM/yyyy");
string finDate = today.AddDays(+6).ToString("dd/MM/yyyy");
literaltest.Text = "Semaine du " + sToday + " au " + finDate;
PlaceHolder1.Controls.Add(new LiteralControl("<br /><br /><br /> kyofu<br /><br />"));
foreach (string sallesel in SallesList)
{
PlaceHolder1.Controls.Add(CreateLinkButton(sallesel + "lkbtn", sallesel, "linkButtonSalle"));
}
Page_init();
}
protected void Page_init()
{
List<LinkButton> allControlsLinkButton = new List<LinkButton>();
GetControlList<LinkButton>(Page.Controls, allControlsLinkButton);
DateTime today = DateTime.Now;
string sToday = DateTime.Now.ToString("dd/MM/yyyy");
// the list of controllers is filled
foreach (var childControl in allControlsLinkButton)
{
if (childControl.CssClass == "linkButtonSalleActive" || childControl.CssClass == "linkButtonSalle")
{
allControlsLinkButtonSalles.Add(childControl);
literal2.Text += " allControlsLinkButtonSalles " + childControl.Text;
}
if (childControl.CssClass == "linkButtonAffichage" || childControl.CssClass == "linkButtonAffichageActive")
{
allControlsLinkButtonAffichages.Add(childControl);
}
if (childControl.CssClass == "linkButtonSemaine" || childControl.CssClass == "linkButtonSemaineActive")
{
allControlsLinkButtonSemaine.Add(childControl);
SemaineSync(childControl);
}
}
literal2.Text += " taille " + allControlsLinkButtonSalles.Count();
//literal2.Text += " Text " + allControlsLinkButtonSalles[1].Text;
foreach (LinkButton value in allControlsLinkButtonSalles)
{
literal2.Text += " <br /> Text " + value.Text;
}
literal2.Text += " First " + allControlsLinkButtonSalles.First().Text;
ListFilmsBySalle(SallesList[0]);
}
private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
where T : Control
{
foreach (Control control in controlCollection)
{
//if (control.GetType() == typeof(T))
if (control is T) // This is cleaner
resultCollection.Add((T)control);
if (control.HasControls())
GetControlList(control.Controls, resultCollection);
}
}
You don't use the static key word properly.
Static variable, is belong to the class itself and don't belong to the current instanace.
Try to use:
public List<LinkButton> allControlsLinkButtonSalles
{
get
{
if(Session["allControlsLinkButtonSalles"] == null)
Session["allControlsLinkButtonSalles"] = new List<LinkButton>();
return (List<LinkButton>) Session["allControlsLinkButtonSalles"];
}
set
{
Session["allControlsLinkButtonSalles"] = value;
}
}
Another isue to take look for is that actually something enter to the list, try to debug it.
Create a Static Class and Create some Attributes and Set your Values to those
Attributes their Whenever you Want you can take it from their if Data has any change
Again set those attributes so that you will get your data from the Class
Public static class Helper
{
public string SOMEPROPERTY
{
get;
set;
}
.
.
.
public List<LinkButton> SOMEPROPERTY
{
get;
set;
}
}
Got the same error as you at first, realised I had no controls in the List you are looking at because I had no LinkButton s with either of the classes that were being searched for with regard to the allControlsLinkButtonSalles list.
I added the CssClass of ""linkButtonSalleActive"" on all of the LinkButton s on my page and I got no debug error.
Add in a check that the list is actually empty for the times where there are no Salle specific links, and you shouldn't have any issues.
Here is the sample code I worked with:
Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" id="literal2" /> <br />
<asp:Label runat="server" id="literaltest" /> <br />
<asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="1"/>
<asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="2"/>
<asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="3"/>
<asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="4"/>
</div>
</form>
</body>
</html>
Default.aspx.cs (modified version of you complete code)
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
//DataTable dt = new data().
public static int Load_Counter = 0;
List<Button> allControlsButton = new List<Button>();
public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();
List<LinkButton> allControlsLinkButtonAffichages = new List<LinkButton>();
List<LinkButton> allControlsLinkButtonSemaine = new List<LinkButton>();
protected void Page_Load(object sender, EventArgs e)
{
literal2.Text += "<br /> counter : " + Load_Counter.ToString();
DateTime today = DateTime.Now;
string sToday = DateTime.Now.ToString("dd/MM/yyyy");
string finDate = today.AddDays(+6).ToString("dd/MM/yyyy");
literaltest.Text = "Semaine du " + sToday + " au " + finDate;
//PlaceHolder1.Controls.Add(new LiteralControl("<br /><br /><br /> kyofu<br /><br />"));
//foreach (string sallesel in SallesList)
//{
// PlaceHolder1.Controls.Add(CreateLinkButton(sallesel + "lkbtn", sallesel, "linkButtonSalle"));
//}
Page_init();
}
protected void Page_init()
{
List<LinkButton> allControlsLinkButton = new List<LinkButton>();
GetControlList<LinkButton>(Page.Controls, allControlsLinkButton);
DateTime today = DateTime.Now;
string sToday = DateTime.Now.ToString("dd/MM/yyyy");
// the list of controllers is filled
foreach (var childControl in allControlsLinkButton)
{
if (childControl.CssClass == "linkButtonSalleActive" || childControl.CssClass == "linkButtonSalle")
{
allControlsLinkButtonSalles.Add(childControl);
literal2.Text += " allControlsLinkButtonSalles " + childControl.Text;
}
if (childControl.CssClass == "linkButtonAffichage" || childControl.CssClass == "linkButtonAffichageActive")
{
allControlsLinkButtonAffichages.Add(childControl);
}
if (childControl.CssClass == "linkButtonSemaine" || childControl.CssClass == "linkButtonSemaineActive")
{
allControlsLinkButtonSemaine.Add(childControl);
//SemaineSync(childControl);
}
}
literal2.Text += " taille " + allControlsLinkButtonSalles.Count();
//literal2.Text += " Text " + allControlsLinkButtonSalles[1].Text;
foreach (LinkButton value in allControlsLinkButtonSalles)
{
literal2.Text += " <br /> Text " + value.Text;
}
/*
* CHANGES HERE
*/
literal2.Text += allControlsLinkButtonSalles.Count > 0 ?
" First " + allControlsLinkButtonSalles.First().Text :
String.Empty;
//ListFilmsBySalle(SallesList[0]);
}
private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
where T : Control
{
foreach (Control control in controlCollection)
{
//if (control.GetType() == typeof(T))
if (control is T) // This is cleaner
resultCollection.Add((T)control);
if (control.HasControls())
GetControlList(control.Controls, resultCollection);
}
}
}
}
As per your code value to the list allControlsLinkButtonSalles is added only if childControl.CssClass == "linkButtonSalleActive",
I am sure there in no value being added, check what you are getting for literal2.Text at:-
literal2.Text += " taille "+ allControlsLinkButtonSalles .Count();
Having said that you should not be using static variables or properties on page unless you have a valid reason to do so.
Related
I cannot see what is wrong here:
Javascript
function showDialogBox(dialogText, atomicId) {
if (dialogText.text == "")
{
dialogText = "¿Do you want to continue?";
}
if ($('#' + atomicId).text() != "")
{
return confirm(dialogText);
}
else
{
return true;
}
}
The Label HTML
<asp:Label ID="AtomicId" ClientIDMode="Static" runat="server" CssClass="datalabel DHidden" />
I checked atomicId param and its "AtomicId" string, but when I check $('#' + atomicId).text, it returns:
alert($('#' + atomicId).text);
What!!??. The text of the Label AtomicId is supposed to be empty the first call.
I updated it with the rest of my code where I call the function. Anyway Shobhit Walia solved it.
protected void GridDecisionsView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Attributes.Add("style", "display:none");
e.Row.Cells[1].Attributes.Add("style", "display:none");
e.Row.Cells[2].Attributes.Add("style", "display:none");
e.Row.Cells[3].Attributes.Add("style", "display:none");
if (e.Row.RowIndex != -1)
e.Row.Attributes.Add("onclick", "if(showDialogBox('" + getDialogString() + "', '" + AtomicId.ID + "')){"
+ CSM.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex.ToString()) + "}");
}
Thanks,
Try with these
function showDialogBox(dialogText, atomicId) {
if (dialogText == "")
{
dialogText = "¿Do you want to continue?";
}
if ($('#' + atomicId).text() != "")
{
return confirm(dialogText);
}
}
I followed this tutorial to allow a user to select a folder from their machine:
http://www.codeproject.com/Articles/21895/Directory-Browsing-in-ASP-Net
The problem is that every time I click on a node, it calls postback, which refreshes the page. So how do I stop postback being called every time?
<asp:TreeView ID="TreeView1" runat="server" Height="326px" ImageSet="XPFileExplorer"
NodeIndent="15" Width="292px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px" />
<LeafNodeStyle ImageUrl="../images/folder.gif" />
</asp:TreeView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeNode onjParent = new TreeNode("C:\\", "C:\\");
onjParent.PopulateOnDemand = true;
TreeView1.Nodes.Add(onjParent);
TreeView1.CollapseAll();
}
error.Visible = false;
TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
}
void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
_browseTextBox.Text = TreeView1.SelectedValue;
}
void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Value.EndsWith("\\"))
{
AddNodes(e.Node.Value, e.Node);
}
}
private TreeNode AddNodes(string path, TreeNode parentNode)
{
FileList objList = new FileList(path, "*.*");
TreeNode node = new TreeNode(path, path);
for (int index = 0; index < objList.Directories.Length; index++)
{
string directory = objList.Directories[index];
TreeNode objChildNode = new TreeNode(directory, path + "\\" + directory + "\\");
objChildNode.PopulateOnDemand = true;
objChildNode.Target = "_blank";
parentNode.ChildNodes.Add(objChildNode);
}
foreach (string file in objList.Files)
{
TreeNode objChildNode = new TreeNode(file, path + "\\" + file);
parentNode.ChildNodes.Add(objChildNode);
}
return node;
}
protected void _browseButton_Click(object sender, ImageClickEventArgs e)
{
TreeView1.Nodes.Clear();
if (UpdateBrowseTextBoxWithSlash())
{
TreeNode onjParent = new TreeNode(_browseTextBox.Text, _browseTextBox.Text);
onjParent.PopulateOnDemand = true;
TreeView1.Nodes.Add(onjParent);
TreeView1.CollapseAll();
}
else
{
error.Visible = true;
error.Text = "Please Enter valid path";
}
}
private bool UpdateBrowseTextBoxWithSlash()
{
if (_browseTextBox.Text.Length != 0)
{
if (
-1 == _browseTextBox.Text.LastIndexOf(".") &&
!_browseTextBox.Text.Substring(_browseTextBox.Text.Length - 1, 1).Equals("/") &&
!_browseTextBox.Text.Substring(_browseTextBox.Text.Length - 1, 1).Equals("\\")
)
{
if (_browseTextBox.Text.Substring(0, 1).Equals("\\") || -1 != _browseTextBox.Text.IndexOf(":\\"))
_browseTextBox.Text += "\\";
else
_browseTextBox.Text += "/";
return System.IO.Directory.Exists(_browseTextBox.Text);
}
else if (_browseTextBox.Text.LastIndexOf(".") > 0)
{
return System.IO.File.Exists(_browseTextBox.Text);
}
}
return true;
}
Have you looked at Imperfect Solution To TreeView? The author provides an interesting idea to omit postback when selecting a node by manually setting the NavigateUrl and SelectAction of each TreeNode. From first glance, it appears logical. The only thing missing is that it is not recursive so it can only handle one node level at a time.
I'm new to C# asp.net and I'm having trouble getting the selected item. Here is the code behind:
List<string> figuritasSelecionadas = new List<string>();
this.lblMensaje.Visible = false;
decimal total = 0;
foreach (ListItem lf in this.ListaFiguritas.Items)
{
if (lf.Selected)
{
figuritasSelecionadas.Add(lf.Text);
total += Decimal.Parse(lf.Value);
}
}
The problem here is that it doesn't matter which item is selected, because when it reaches the if the first item is marked as true. And I have no idea why it's doing this.
Here is how I load the ListBox:
private void cargarFiguritas()
{
List<Figurita> figuritas = Sistema.Instancia.figuritasQueFaltan(usuarioActivo);
this.ListaFiguritas.DataSource = figuritas;
this.ListaFiguritas.DataValueField = "Precio";
this.ListaFiguritas.DataTextField = "NumeroFigurita";
this.ListaFiguritas.DataBind();
Session["ListaFiguritas"] = figuritas;
}
protected void Page_Load(object sender, EventArgs e)
{
usuarioActivo = (string)Session["nombreUsuario"];
this.lblUsuario.Text = (string)Session["nombreUsuario"] + " tiene un total de: " + Session["Monedas"].ToString() + " monedas";
if (!IsPostBack)
{
cargarFiguritas();
}
}
Here is the aspx code:
<asp:ListBox ID="ListaFiguritas" runat="server" Height="180px" Width="110px" SelectionMode="Single">
</asp:ListBox>
I have populated my checkboxlist on the fly via callback like this:
<dx:ASPxComboBox ID="ASPxComboBox_Prot" runat="server" DataSourceID="SqlDataSource_Prot"
TextField="LIBELLE" ValueField="NO_PROT" ValueType="System.Int32">
<ClientSideEvents SelectedIndexChanged="function(s, e) { cbp_ProtOrdos.PerformCallback(s.GetValue());}" />
</dx:ASPxComboBox>
</td>
</tr>
</table>
<dx:ASPxCallbackPanel ID="ASPxCallbackPanel_ProtOrdo" runat="server"
ClientInstanceName="cbp_ProtOrdos" OnCallback="cbp_ProtOrdo_Callback">
<PanelCollection>
<dx:PanelContent>
<dx:ASPxCheckBoxList ID="CheckBoxList_Ordo" runat="server" ClientInstanceName="CheckBoxList_Ordo" ValueType="System.Int32" TextField="LIBELLE" ValueField="NO_ORDO">
</dx:ASPxCheckBoxList>
<dx:ASPxButton ID="ASPxButton_ProtOrdoGen" runat="server"
Text="Générer ordonnance & Planifier pour infirmier"
OnClick="ASPxButton_ProtOrdoGen_Click"
EnableDefaultAppearance="false" BackColor="Yellow" CssClass="bt" Theme="BlackGlass" ForeColor="Black">
</dx:ASPxButton>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
And on server side code:
protected void cbp_ProtOrdo_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
var panel = sender as ASPxCallbackPanel;
var cblist = panel.FindControl("CheckBoxList_Ordo") as ASPxCheckBoxList;
cblist.DataSource = Outils.Get_ProtOrdo(ASPxComboBox_Prot.Value.ToString());
cblist.DataBind();
}
It works fine, but now I want to get the value that had been checked by the user. So I add the button to do that.
protected void ASPxButton_ProtOrdoGen_Click(object sender, EventArgs e)
{
//TabPage oPage = ASPxPageControl_DosSoin.TabPages.FindByName("Surveillance");
//ASPxPanel oPanel = (ASPxPanel)oPage.FindControl("ASPxPanel_ListSurveil");
//ASPxRoundPanel oRoundPnl = (ASPxRoundPanel)oPanel.FindControl("ASPxRoundPanel_ProtOrdo");
//ASPxCallbackPanel ocbpPanel = (ASPxCallbackPanel)oRoundPnl.FindControl("ASPxCallbackPanel_ProtOrdo");
//ASPxCheckBoxList cblist = (ASPxCheckBoxList)ocbpPanel.FindControl("CheckBoxList_Ordo") as ASPxCheckBoxList;
List<string> selectItems_Ordo = new List<string>();
foreach (var oItem in CheckBoxList_Ordo.Items)
{
ListEditItem oNewChk = (ListEditItem)oItem;
if (oNewChk.Selected)
{
selectItems_Ordo.Add( oNewChk.Value.ToString());
}
}
foreach (var oItem in selectItems_Ordo)
{
if (DossierDuSoins.check_doublon_ordo(oItem.ToString(), Soin_Id) == 0)
DossierDuSoins.RamenerVal(DossierDuSoins.GetLibOrdo(oItem.ToString()), Soin_Id, oItem.ToString());
}
string TempId = "";
if (selectItems_Ordo.Count == 0)
{
lbl_err.Text = "Pas de médicament de sélectionné";
}
else
{
foreach (string selectItemId in selectItems_Ordo)
{
if (TempId != "")
TempId += ",";
TempId += selectItemId.ToString();
}
string AdrUrl = "Print_Ordo.aspx?SoinId=" + Soin_Id + "&SelId=" + TempId;
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", AdrUrl));
}
}
The problem is that I can not get my checked value. Is that because the postback destroys all checkboxlists that I had constructed on the fly ?
Try this instead of using vars for selecting your checkbox list
foreach (ListItem yourItem in YourCheckBoxList.Items)
{
if (item.Selected)
{
// If the item is selected, Add to your list/ save to DB
}
else
{
// If item is not selected, do something else.
}
}
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;
}
Do you really need a list for this? You could do...
if(lblFName.Text.Equals(String.Empty))
{
lblFName.Text = value;
}
else if(lblFName1.Text.Equals(String.Empty))
{
lblFName1.Text = value;
}//and so on...
If a postback is firing when you hit the enter button. Then you need to handle rebuilding the list in the Page_Load. Something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
recipientList = (List<string>)ViewState["recipientList"];
//now load the list
}
}