如何将文本框中的用户输入添加到数组或arraylist中

时间:2013-03-08 15:27:24

标签: c# arrays arraylist

我正在创建礼品卡应用程序,用户将收件人信息输入文本框。该应用程序允许用户添加另一个礼品卡(最多5次)。我试图找到一种方法将文本框输入添加到我可以稍后调用的数组或arrayList中,并在他们的“购物车”中显示该信息。该应用程序在C#中使用Visual Studio 2012中的webform平台。到目前为止,对于arraList,我有:

private ArrayList arrayList = new ArrayList();

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnEnter_Click(object sender, EventArgs e)
{
   arrayList.Add(txtFName.Text);

   if (arrayList.Count == 1)
   {
      lblFName.Text = arrayList[0].ToString();
   }

   if (arrayList.Count == 2)
   {
      lblFName.Text = arrayList[0].ToString();
      lblFName1.Text = arrayList[1].ToString();
   }
}

我相信我只需要一种方法来将输入增加到数组中,但我无法弄清楚如何做到这一点。任何有关其他途径的帮助或建议都会受到严重影响

以下是收件人信息面板的代码以及购物车的面板。 //             

收件人信息:

                         

需要获得Visa预付卡的人的姓名。该名称将显示在卡片上。

                                                  *名字:                                                                                                              
                
                                     中间初始:                                                                                         
                
                                     *姓:                                                                                                              
                
                                     后缀:                                                                                                         小                         高级                         II                         III                         IV                                                                            
                
                名字,中间名,姓氏和后缀的组合不能超过26个字符                 
                         

礼品卡信息:

                         
            必须是介于$ 25.00和$ 500.00之间的整数,没有美元符号或小数。             
            
                                                  *输入金额:                                                                                                              
                
                                     个性化消息:                                                                                                         祝一切顺利                         一路顺风                         祝贺                         祝贺Grad                         祝好运                         周年快乐                         生日快乐                         玩得开心                         只是因为                         刚刚结婚                         圣诞节快乐                         季节的问候                         谢谢                                                       
                
                或选择                                                   
                                     自定义消息:                                                                                                              
                
                                 
                
                                                                            
                            
  • CANCEL
  •                         
  •                             BACK
  •                         
  •                             继续
  •                     
                                      //

    //<asp:Panel ID="pnlCart" runat="server" Width="100%" Visible="false">
        <asp:Panel ID="Panel1" runat="server" GroupingText="Card Choice 1" ForeColor="DarkBlue" Font-Bold="True" BorderStyle="NotSet" BorderColor="#E0E0E0">
            <div class="container">
                <div class="pair">
                    <div class="labelLeftBold">
                        Amount:
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartAmount" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Card Design: 
                    </div>
                    <div class="imgCard">
                        <asp:Image ID="imgCardChoice1" runat="server" />
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartDesign" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Name on Card: 
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartName" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Message on Card: 
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartMessage" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                </div>
            </div>
        </asp:Panel>
        <br />
        <div class="container1">
            <asp:Panel ID="Panel2" runat="server" GroupingText="Card Choice 2" ForeColor="DarkBlue" Font-Bold="True" BorderStyle="NotSet" BorderColor="#E0E0E0">
                <div class="pair">
                    <br />
                    <div class="labelLeftBold">
                        Amount: 
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartAmount1" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Card Design: 
                    </div>
                    <div class="imgCard">
                        <asp:Image ID="imgCardChoice2" runat="server" />
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartDesign1" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Name on Card: 
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartName1" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                    <div class="labelLeftBold">
                        Message on Card: 
                    </div>
                    <div class="fieldRight">
                        <asp:Label ID="lblCartMessage1" runat="server" Font-Bold="true"></asp:Label>
                    </div>
                    <br />
                    <br />
                </div>
           // </asp:Panel>

4 个答案:

答案 0 :(得分:0)

如果只有5个值,那么你可以这样做。

string[] values = new string[5];

values[0] = lblFName.Text;
values[1] = lblFName2.Text; // etc

然后设置文本框值使用此

lblFName.Text = values[0];
lblFName2.Text = values[1];

如果values [i]等于null,则无关紧要,因为string可以是空值。

答案 1 :(得分:0)

为什么不使用List

private void button1_Click(object sender, EventArgs e)
{
   Control[] aryCtrl = new Control[] { textBox1, textBox2, textBox3, dropdownlist1, dropdownlist2, dropdownlist3 };
   List<string> list = new List<string>();

   foreach (Control ctrl in aryCtrl)
   {
      list.Add(textbox1.Text);
      list.Add(textbox2.Text);
      list.Add(textbox3.Text);
      list.Add(dropdownlist1.Text);
      list.Add(dropdownlist2.Text);
      list.Add(dropdownlist3.Text);
   }
}

我创建了一个Control[]数组:

Control[] aryCtrl = new Control[] { textBox1, textBox2, textBox3, comboBox1, comboBox2, comboBox3 };

我在foreach循环中使用它。接下来,正如我在原始答案中提到的,我创建了一个List对象来存储文本框/下拉列表中的数据。最后,使用foreach循环,我遍历Control[]数组并添加每个控件的.text值。

编辑。这应该适合你,OP!

答案 2 :(得分:0)

行。我想我明白你在这里要做什么。

创建一个简单的类来存储数据。

 Class Recipient
 {
      string Name { get; set; }
      string CreditCardName { get; set; }
      string Amount { get; set; }
      string Message { get; set; }
 }

显然,您可以将数据类型更改为您想要的任何值(即数量可能是一个双倍)。

现在您拥有控制数组(技术上甚至不需要)和在button1_Click事件之外声明的收件人列表。

Control[] aryCtrl = new Control[] { 
                                   txtName, 
                                   cbxCreditCardName, 
                                   txtAmount, 
                                   txtMessage };

var recipients = new List<Recipient>();

private void button1_Click(object sender, EventArgs e)
{
    if (recipients.Count < 5)
    {
       var recipient = new Recipient();

       recipient.Name = txtName.Text;
       recipient.CreditCardName = cbxCreditCardName.SelectedValue;
       recipient.CreditCardNumber = txtCreditCardNumber.Text;
       recipient.Amount= txtAmount.Text;
       recipient.Message= txtMessage.Text;

       recipients.Add(recipient);
    }
    else {
         //Provide user with message telling them that they can only have up to 5 recipients.
    }
}

有一种方法可以从列表中删除收件人。

答案 3 :(得分:0)

感谢所有帮助过的人。我使用ViewState和其他一些修改解决了这个问题。

public partial class Test : System.Web.UI.Page
{
    [Serializable]
    class Recipient
    {
        public string name { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        Recipient recipients = new Recipient();

        List<string> recipient = (List<string>)ViewState["recipientList"];


        if (recipient == null)
        {
            recipient = new List<string>();
        }
        recipients.name = txtFName.Text.Trim();
        recipient.Add(recipients.name);
        ViewState["recipientList"] = recipient;

        if (recipient.Count == 1)
        {
            lblFName.Text = recipient[0];
        }

        if (recipient.Count == 2)
        {
            lblFName1.Text = recipient[1];
        }

    }