我正在制作一个程序,它接受用户输入并将其填充到列表中。这部分工作很好。但是,用户需要编辑或删除他/她的输入的能力。我无法弄清楚如何从列表中删除一个项目。下面是填充列表代码:我需要从列表中删除项目
[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);例如,这些都没有奏效。所以这是我的问题,任何帮助将不胜感激。由于
你覆盖在'ViewState'相同的元素: 使用这样的' – 2013-03-14 16:39:59
这个例子让我很头疼 – TalentTuner 2013-03-14 16:41:37
我相信你有你的理由,但是创建'List'比每个字段的单个列表更不友善吗? –
2013-03-14 16:42:16