我有一个.aspx应用程序,用户输入一个名称,并将该名称添加到列表中。这最多可以完成五次。当用户单击该按钮时,输入的第一个名称将显示在第一个标签中。当用户输入另一个名称并单击该按钮时,第一个标签保持不变,下一个标签显示新名称,依此类推。我的问题是在PostBack上重置列表。我试图使用ViewState来帮助解决这个问题,但没有成功。非常感谢任何帮助。
编辑:我得到了它的工作,所以谢谢大家的帮助。仍有很大的改进空间,但这是一个很好的起点。
[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];
}
}
答案 0 :(得分:0)
你真的需要一份清单吗?你可以做......
if(lblFName.Text.Equals(String.Empty))
{
lblFName.Text = value;
}
else if(lblFName1.Text.Equals(String.Empty))
{
lblFName1.Text = value;
}//and so on...
答案 1 :(得分:0)
我不确定收件人类的目的。无论如何,你想在添加收件人之前实例化收件人列表。
<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;
}
答案 2 :(得分:0)
如果按下回车按钮后会触发回发。然后,您需要处理重新构建Page_Load中的列表。像这样:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
recipientList = (List<string>)ViewState["recipientList"];
//now load the list
}
}