我想问一个关于页面生成的问题 当我按了网面上一个button按扭的时候重新生成的这个网页会不会在动行page_load这个方法我有一点迷获 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 所以如果想在提交后不做什么事的话就加上if(!this.IsPostBack){ //第一次才做的事。} private void Page_Load(object sender, System.EventArgs e) { DataSet ds=new DataSet(); SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1); sa.Fill(ds,"wocaonima"); DataRow dr=ds.Tables["wocaonima"].NewRow(); dr["a"]=1; dr["b"]="8"; dr["c"]="8"; ds.Tables[0].Rows.Add(dr); //SqlCommandBuilder sb=new SqlCommandBuilder(sa); //sa.InsertCommand=sb.GetInsertCommand(); //SqlCommand sc=new SqlCommand("delete wocao",sqlConnection1); //sa.InsertCommand=sc; //sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=PAN;packet size=4096;user id=sa;data source=PAN;persist security i" + "nfo=False;initial catalog=test"; // // sqlCommand1 // this.sqlCommand1.CommandText = "SELECT * FROM wocao"; this.sqlCommand1.Connection = this.sqlConnection1; // // sqlDataAdapter1 // this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1; this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { DataSet ds=new DataSet(); SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1); sa.Fill(ds,"wocaonima"); DataRow dr=ds.Tables["wocaonima"].NewRow(); dr["a"]=1; dr["b"]="8"; dr["c"]="8"; ds.Tables[0].Rows.Add(dr); SqlCommandBuilder sb=new SqlCommandBuilder(sa); sa.InsertCommand=sb.GetInsertCommand(); sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } 1.有什么问题?2.把绑定datagrid的程序做一个方法,可多次调用。 把Page_Load函数里的代码,全放到if(!ispostback)里面,再去执行一下,效果应该有所不同啦 if(!this.IsPostBack){ 放这里边}页面每次回发都会执行Page_load事件. if(!Page.IsPostBack){ 内容;}以后里面的内容就不会每次提交都执行了 不管你点button, 还是dropdownlist(如果autopostback=true)等,都会首先执行page_load,也就是说,只要进入页面必执行page_load,可以用if(!IsPostBakc){...},楼上说了. Click the link to solve your problem.Good luck! 请问 Datatable 和 DataSet 是在客户端运行还是服务器运行? 正则表达式获取html代码里的数据 请教eval如何绑定数组 asp.net 可不可以上传整个文件夹! 请问这个"/"怎么处理? C#和VB能共存吗? 想在BODY第一次启动时显示某函数 谁有ASP.NET C#版的建站系统源码,可否拿出来SHARE一下? 关于ASP。NET调试问题 我的水晶报表生成的图表为什么不能看能设置图表的输出路径吗??? 如何向数据库可存入'这个符号,比如string aa="'hao'"? 数据混乱问题
{
//第一次才做的事。
}
{
DataSet ds=new DataSet();
SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1);
sa.Fill(ds,"wocaonima");
DataRow dr=ds.Tables["wocaonima"].NewRow();
dr["a"]=1;
dr["b"]="8";
dr["c"]="8";
ds.Tables[0].Rows.Add(dr);
//SqlCommandBuilder sb=new SqlCommandBuilder(sa);
//sa.InsertCommand=sb.GetInsertCommand();
//SqlCommand sc=new SqlCommand("delete wocao",sqlConnection1);
//sa.InsertCommand=sc;
//sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds;
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=PAN;packet size=4096;user id=sa;data source=PAN;persist security i" +
"nfo=False;initial catalog=test";
//
// sqlCommand1
//
this.sqlCommand1.CommandText = "SELECT * FROM wocao";
this.sqlCommand1.Connection = this.sqlConnection1;
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1);
sa.Fill(ds,"wocaonima");
DataRow dr=ds.Tables["wocaonima"].NewRow();
dr["a"]=1;
dr["b"]="8";
dr["c"]="8";
ds.Tables[0].Rows.Add(dr);
SqlCommandBuilder sb=new SqlCommandBuilder(sa);
sa.InsertCommand=sb.GetInsertCommand();
sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
2.把绑定datagrid的程序做一个方法,可多次调用。
{
放这里边
}页面每次回发都会执行Page_load事件.
{
内容;
}
以后里面的内容就不会每次提交都执行了