我用这段代码创建了一个类:
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;
}
返回此列表以与网格绑定(bind)。代码是:
List<Customer> lstCustomer = new List<Customer>();
lstCustomer = Generate_Data();
GridView1.DataSource = lstCustomer;
GridView1.DataBind();
我的问题是:
我在 aspx 页面中添加了 4 个文本框和一个按钮,名称为:
Id,Name,FatherName,Email
当我点击按钮时,我想将文本框的新值添加到 gridview1 行。我想动态地向 gridview 添加一行。如果我定义了一个空的 gridview,如何将我的文本框值添加到 gridview 行?不是 equal method with question1 吗?
最佳答案
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;
}
}
关于c# - 将新行数据添加到 gridview asp.net c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681206/