7

我用这段代码做了一个类:

public class Customer
{
    public Customer() { }
    public Customer(Customer cust)
    {
        ID = cust.ID;
        Name = cust.Name;
        FatherName = cust.FatherName;
        Email = cust.Email;
    }
    public int ID { get; set; }
    public string Name { get; set; }
    public string FatherName { get; set; }
    public string Email { get; set; }
}

并创建了这个函数来加载一些数据的列表:

public List<Customer> Generate_Data()
{
    List<Customer> lstCustomer = new List<Customer>();
    Customer customer = new Customer();

    customer.ID = 1;
    customer.Name = "John Cena";
    customer.FatherName = "John";
    customer.Email = "cena@gmail.com";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 2;
    customer.Name = "Mokesh";
    customer.FatherName = "Rajnikant";
    customer.Email = "mokesh@gmail.com";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 3;
    customer.Name = "Bilal Ahmad";
    customer.FatherName = "Kashif";
    customer.Email = "bilal@gmail.com";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 4;
    customer.Name = "Chin Shang";
    customer.FatherName = "Shang Woe";
    customer.Email = "chinshang@gmail.com";
    lstCustomer.Add(new Customer(customer));

    return lstCustomer;
}

返回此列表以与网格绑定。代码是:

List<Customer> lstCustomer = new List<Customer>();
lstCustomer = Generate_Data();
GridView1.DataSource = lstCustomer;
GridView1.DataBind();

我的问题是:

  1. 我将 4 个文本框和一个按钮添加到带有名称的 aspx 页面:Id,Name,FatherName,Email 当我单击按钮时,我想将文本框的新值添加到 gridview1 行。我想动态地向 gridview 添加一行。

  2. 如果我定义了一个空的 gridview,我如何将我的文本框值添加到 gridview 行?不是与 question1 相等的方法吗?

4

4 回答 4

6

ASPX:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server"/>

后面的代码:

public class Customer
{
    public Customer() { }
    public Customer(Customer cust)
    {
        ID = cust.ID;
        Name = cust.Name;
        FatherName = cust.FatherName;
        Email = cust.Email;
    }
    public int ID { get; set; }
    public string Name { get; set; }
    public string FatherName { get; set; }
    public string Email { get; set; }
}
protected void Button1_Click(object sender, EventArgs e)
{
    List<Customer> lstCustomer = new List<Customer>();
    if (Session["dt"] != null)
    {
      lstCustomer = (List<Customer>)Session["dt"];
    }
    Customer customer = new Customer();
    customer.ID = int.Parse(TextBox1.Text);
    customer.Name = TextBox2.Text;
    customer.FatherName = TextBox2.Text;
    customer.Email = TextBox2.Text;
    lstCustomer.Add(new Customer(customer));
    GridView1.DataSource = lstCustomer;
    GridView1.DataBind();
    Session["dt"] = lstCustomer;
}

更新!

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Customer> lstCustomer = new List<Customer>();
        Customer customer = new Customer();

        customer.ID = 1;
        customer.Name = "John Cena";
        customer.FatherName = "John";
        customer.Email = "cena@gmail.com";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 2;
        customer.Name = "Mokesh";
        customer.FatherName = "Rajnikant";
        customer.Email = "mokesh@gmail.com";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 3;
        customer.Name = "Bilal Ahmad";
        customer.FatherName = "Kashif";
        customer.Email = "bilal@gmail.com";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 4;
        customer.Name = "Chin Shang";
        customer.FatherName = "Shang Woe";
        customer.Email = "chinshang@gmail.com";
        lstCustomer.Add(new Customer(customer));
        Session["dt"] = lstCustomer;

    }
}
于 2013-09-08T07:06:31.340 回答
2

您需要在回发期间保留您的收藏。根据您的收藏有多大,您可以考虑将其保存在视图状态、会话等中。

List<Customer> lstCustomer = new List<Customer>();
lstCustomer = Generate_Data();
ViewState["Customers"] = lstCustomer;
GridView1.DataSource = lstCustomer;
GridView1.DataBind();

因此,当用户用数据填充文本框并启动回发时,您可以创建一个新的客户对象并将其添加到现有集合中。然后将数据重新绑定到网格。

Customer customer = new Customer(){ID=int.Parse(TextBox1.Text), Name = TextBox2.Text, 
    FatherName = TextBox2.Text, Email = TextBox2.Text }
var lstCustomer = ViewState["Customers"] as List<Customers>;
    lstCustomer.Add(customer);

请注意,您需要将该[Serializable]属性添加到您的Customer类中。

于 2013-09-08T07:18:25.733 回答
0

您可以简单地添加AutoGenerateInsertButton="True"<asp:GridView>标签。

于 2013-09-08T06:50:45.763 回答
0

所以,我会这样做。首先,我会扩展 lstCustomer 的范围,以便它通过回发持续存在。

然后在按钮事件处理程序中,我会写这样的东西

//Here we create the object
Customer customer = new Customer();
customer.foo = txtFoo.Text;
customer.bar = txtBar.Text;
//....

//Then we add it to the list
lstCustomer.Add(customer);

//and databind on it again
GridView1.DataSource = lstCustomer;
GridView1.DataBind();

如果您想让用户对数据执行一些操作(或者您想持久化到数据库或其他内容),这也很有帮助。

于 2013-09-08T06:53:03.040 回答