【问题标题】:How to display data from Textbox to a Gridview using Generic List (Typed List)?如何使用通用列表(类型列表)将数据从文本框显示到 Gridview?
【发布时间】:2013-02-18 09:43:49
【问题描述】:

我创建了两个文本框,用于在 Visual Studio 2010 上使用 C# 在基于 Web 的 ASP.NET 应用程序中输入员工的名字和姓氏。有一个按钮。当我单击它时,我在 TextBoxes 中输入的值应该显示在 Gridview 中,而不会将这些值存储在数据库中。我已经使用 ArrayList 实现了它,如下所示。但现在我想使用 Generic List (Typed List) 来实现它。我怎样才能做到这一点?能否提供一个示例代码来执行上述功能?

如何调整下面给出的代码,以使用通用列表(类型化列表)实现功能?

**

    protected void btnTextDisplay_Click(object sender, EventArgs e)
    {
        ArrayList arr;

        if (Session["array"] == null)
        {
            arr = new ArrayList();
        }
        else
        {
            arr = (ArrayList)Session["array"];
        }

        arr.Add(txtName.Text + "," + txtCity.Text); //store textbox values in the array list
        Session["array"] = arr;

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("City");

        for (int i = 0; i < arr.Count; i++)
        {
            string[] arrVal;
            arrVal = arr[i].ToString().Split(',');
            dt.Rows.Add(arrVal[0], arrVal[1]);
        }

        gvDisplay.DataSource = dt;
        gvDisplay.DataBind();
    }

**

【问题讨论】:

    标签: c# asp.net list generic-list


    【解决方案1】:

    您可以使用List&lt;string&gt; 代替ArrayList

    protected void btnTextDisplay_Click(object sender, EventArgs e)
        {
           List<string> list;
    
            if (Session["list"] == null)
            {
                list = new List<string>();
            }
            else
            {
                list = (List<string>)Session["list"];
            }
    
            list.Add(txtName.Text + "," + txtCity.Text); //store textbox values in the array list
            Session["list"] = list;
    
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("City");
    
            for (int i = 0; i < list.Count; i++)
            {
                string[] arrVal;
                arrVal = list[i].ToString().Split(',');
                dt.Rows.Add(arrVal[0], arrVal[1]);
            }
    
            gvDisplay.DataSource = dt;
            gvDisplay.DataBind();
        }
    

    更好的方法可能是:

    如果你将一个类定义为:

    class Employee
    {
        public string FirstName { get; set; }
        public string City { get; set; }
    }
    

    然后:

        protected void btnTextDisplay_Click(object sender, EventArgs e)
        {
            List<Employee> list;
            if (Session["list"] == null)
            {
                list = new List<Employee>();
            }
            else
            {
                list = (List<Employee>)Session["list"];
            }
            list.Add(new Employee() { FirstName = txtName.Text, City = txtCity.Text }); //store textbox values in the array list
            Session["list"] = list;
            gvDisplay.DataSource = list; //directly bind the list to the grid
            gvDisplay.DataBind();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多