我需要从列表中删除项目

时间:2013-03-14 16:38:27

标签: c# asp.net list viewstate

我正在制作一个程序,它接受用户输入并将其填充到列表中。那部分工作得很好。但是,用户需要能够编辑或删除他/她的输入。我无法弄清楚如何从列表中删除项目。以下是填充列表的代码:

[Serializable]
class Recipient
{
    public string Fname { get; set; }
    public string MInit { get; set; }
    public string Lname { get; set; }
    public string Suffix { get; set; }
    public string Amount { get; set; }
    public string Message { get; set; }
    public string Custom { get; set; }
    public string CardType { get; set; }
} protected void btnToCart_Click(object sender, EventArgs e)
{
    if (ValidateInput("Card Information"))
    { SetUI("Your Shopping Cart"); }
    else
    {
        return;
    }

    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"];
    List<string> Suffix = (List<string>)ViewState["recipientList"];
    List<string> Amount = (List<string>)ViewState["recipientList"];
    List<string> Message = (List<string>)ViewState["recipientList"];
    List<string> Custom = (List<string>)ViewState["recipientList"];
    List<string> CardType = (List<string>)ViewState["recipientList"];

    if (FName == null && MInit == null && LName == null && Suffix == null && Amount == null &&
            Message == null && Custom == null && CardType == null)
    {
        FName = new List<string>();
        MInit = new List<string>();
        LName = new List<string>();
        Suffix = new List<string>();
        Amount = new List<string>();
        Message = new List<string>();
        Custom = new List<string>();
        CardType = new List<string>();
    }

    recipients.Fname = txtFName.Text;
    recipients.MInit = txtMInit.Text;
    recipients.Lname = txtLName.Text;
    recipients.Suffix = ddlSuffix1.SelectedItem.ToString();
    recipients.Amount = txtAmount.Text;
    recipients.Message = ddlMessage.SelectedItem.ToString();
    recipients.Custom = txtCustom.Text;
    recipients.CardType = lblImage.Text;
    FName.Add(recipients.Fname);
    MInit.Add(recipients.MInit);
    LName.Add(recipients.Lname);
    Suffix.Add(recipients.Suffix);
    Amount.Add(recipients.Amount);
    Message.Add(recipients.Message);
    Custom.Add(recipients.Custom);
    CardType.Add(recipients.CardType);
    ViewState["recipientList"] = FName;
    ViewState["recipientList"] = MInit;
    ViewState["recipientList"] = LName;
    ViewState["recipientList"] = Suffix;
    ViewState["recipientList"] = Amount;
    ViewState["recipientList"] = Message;
    ViewState["recipientList"] = Custom;
    ViewState["recipientList"] = CardType;

    if (FName.Count == 1 && MInit.Count == 1 && LName.Count == 1 && Suffix.Count == 1)
    {
        lblCartName.Text = FName[0] + " " + MInit[0] + " " + LName[0] + " " + Suffix[0];
        lnkEdit1.Visible = true;
    }

    if (Amount.Count == 1 && Message.Count == 1 && Custom.Count == 1)
    {
        lblCartAmount.Text = "$" + Amount[0] + ".00";
        if (txtCustom.Text == string.Empty)
        {
            lblCartMessage.Text = Message[0];
        }
        else
        {
            lblCartMessage.Text = Custom[0];
        }
    }

是的还有更多内容,但无论如何,一旦用户点击下一个按钮,就会向用户显示包含所有输入信息的摘要。表单上还有两个链接按钮,用户可以选择编辑或删除。我尝试过各种变体:

FName.Remove(recipients.fname);和FName.RemoveAt(0);例如,这些都没有奏效。所以这是我的问题,任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

喜欢@ rich.okelly写道,你覆盖了你的ViewState: 使用这样的东西:

List<string> FName = (List<string>)ViewState["recipientListFName"];
List<string> MInit = (List<string>)ViewState["recipientListMInit"];
List<string> LName = (List<string>)ViewState["recipientListLName"];
List<string> Suffix = (List<string>)ViewState["recipientListSuffix"];
List<string> Amount = (List<string>)ViewState["recipientListAmount"];
List<string> Message = (List<string>)ViewState["recipientListMessage"];
List<string> Custom = (List<string>)ViewState["recipientListCustom"];
List<string> CardType = (List<string>)ViewState["recipientListCardType"];