我用这段代码创建了一个类:
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();
我的问题是:
我在aspx页面添加了4个文本框和一个按钮,名称为:Id,Name,FatherName,Email
当我单击按钮时,我想将文本框的新值添加到gridview1行。我想动态地向gridview添加一行。
如果我定义一个空的gridview,如何将我的文本框值添加到gridview行?与问题1不一样的方法吗?
答案 0 :(得分: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;
}
}
答案 1 :(得分: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
类。
答案 2 :(得分:0)
您只需将AutoGenerateInsertButton="True"
添加到<asp:GridView>
代码即可。
答案 3 :(得分: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();
如果您想让用户对数据执行某些操作(或者您希望持久保存到数据库或其他内容),这也会有所帮助。