0

我有一个 .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];
        }

    }
4

3 回答 3

0

你真的需要一个清单吗?你可以做...

if(lblFName.Text.Equals(String.Empty))
{
    lblFName.Text = value;
}
else if(lblFName1.Text.Equals(String.Empty))
{
    lblFName1.Text = value;
}//and so on...
于 2013-03-12T18:53:19.780 回答
0

我不确定 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;
}
于 2013-03-12T20:58:30.583 回答
0

如果在您按下回车按钮时正在触发回发。然后您需要处理在 Page_Load 中重建列表。像这样的东西:

 protected void Page_Load(object sender, EventArgs e)
 {
    if (!IsPostBack)
    {
        recipientList = (List<string>)ViewState["recipientList"];

        //now load the list
    }
  }
于 2013-03-12T21:00:48.157 回答