将记录添加到数据集asp.net
本文关键字:asp net 数据集 记录 添加 | 更新日期: 2023-09-27 17:59:10
当我试图在前一次崩溃的数据集中添加一条记录时,我有一个表单,每次点击按钮1后,我都会将数据添加到数据集中,每次我只找到最后一条记录这是我的代码
public partial class _Default : System.Web.UI.Page
{
private DataTable dt=new DataTable("manager");
private DataSet data_set=new DataSet("Test");
private static int _stagiaireid = 0;
public static int StagiaireID { get { return _stagiaireid++; } }
protected void Page_Load(object sender, EventArgs e)
{
//dt = new DataTable("manager");
dt.Columns.Add("id ", typeof(int));
dt.Columns.Add("electro ", typeof(string));
dt.Columns.Add("sn", typeof(string));
dt.Columns.Add("date ", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("marque ", typeof(string));
DropDownList1.Items.Add(new ListItem("Siera", "Siera"));
DropDownList1.Items.Add(new ListItem("AB","AB"));
DropDownList1.Items.Add(new ListItem("CD","CD"));
data_set.Tables.Add(dt);
GridView1.DataSource = data_set.Tables["manager"];
}
protected void Button1_Click(object sender, EventArgs e)
{
String electro = TextBox1.Text;
String sn = TextBox2.Text;
String date = TextBox3.Text;
String email = TextBox4.Text;
String marque = DropDownList1.SelectedValue.ToString() ;
if (aide.nom_pre(email)==true)
{
Label1.Text = "Email Juste";
}
else
{
Label1.Text = "Email Faux";
return;
}
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
// dt.Rows.Add(StagiaireID,electro, sn, date, email, marque);
GridView1.DataBind();
}
当然,您只会得到最后一条记录,因为您在每次回发中都要创建和填充DataSet
和DataTable
,并且您没有将DataSet
存储在会话状态中,也没有绑定GridView
将您的代码更改为:
private DataTable dt = new DataTable("manager");
private DataSet data_set = new DataSet("Test");
private static int _stagiaireid = 0;
public static int StagiaireID { get { return _stagiaireid++; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("id ", typeof(int));
dt.Columns.Add("electro ", typeof(string));
dt.Columns.Add("sn", typeof(string));
dt.Columns.Add("date ", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("marque ", typeof(string));
data_set.Tables.Add(dt);
GridView1.DataSource = data_set.Tables["manager"];
this.ViewState["ds"] = data_set;
GridView1.DataBind();
}
else
{
data_set = (DataSet)ViewState["ds"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String electro = TextBox1.Text;
String sn = TextBox2.Text;
String date = TextBox3.Text;
String email = TextBox4.Text;
String marque = DropDownList1.SelectedValue.ToString();
if (aide.nom_pre(email) == true)
{
Label1.Text = "Email Juste";
}
else
{
Label1.Text = "Email Faux";
return;
}
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
GridView1.DataSource = data_set.Tables["manager"];
GridView1.DataBind();
this.ViewState["ds"] = data_set;
}
了解ASP.NET页面生命周期和本教程是很好的
问题是您没有在post-back中持久化数据集。因此,请保持数据集处于会话状态。请参阅ASP.NET 中的状态管理机制
在将数据表绑定到网格视图之前,对已添加行的数据表调用AcceptChanges()
方法,然后再次将DataSource
分配给具有该表的网格视图。
data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
data_set.Tables["manager"].AcceptChanges();
GridView1.DataSource = data_set.Tables["manager"];
GridView1.DataBind();