下面是一个学生退课的程序,为什么在运行的时候该程序总被执行两次???是把它放在page_load里面的原因嘛?请各位告诉我详细的原因,谢谢。
namespace class_select
{
public class delclas:System.Web.UI.Page
{
private void Page_Load(object sender,System.EventArgs e)
{
string getcno = Request["cno"];
try
{
SqlConnection mycon = new SqlConnection("server=localhost;database=mytest;uid=sa;pwd=sa;connect timeout=120");
string delsql="delete from sco where cno='"+getcno+"' and sno='"+Session["sno"]+"'";
SqlCommand cmd = new SqlCommand(delsql,mycon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
string str="update csee set cpeo=cpeo-1 where cno='"+getcno+"'";
cmd = new SqlCommand(str,mycon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
string script = "";
script += "<script language=javascript>alert(\"退课成功!\");";
script += " window.opener.location.reload();";
script += " window.close();</script>";
Response.Write(""+script+"");
}
catch
{
Response.Write("<script>alert(\"请先注册\")</script>");
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
namespace class_select
{
public class delclas:System.Web.UI.Page
{
private void Page_Load(object sender,System.EventArgs e)
{
string getcno = Request["cno"];
try
{
SqlConnection mycon = new SqlConnection("server=localhost;database=mytest;uid=sa;pwd=sa;connect timeout=120");
string delsql="delete from sco where cno='"+getcno+"' and sno='"+Session["sno"]+"'";
SqlCommand cmd = new SqlCommand(delsql,mycon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
string str="update csee set cpeo=cpeo-1 where cno='"+getcno+"'";
cmd = new SqlCommand(str,mycon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
string script = "";
script += "<script language=javascript>alert(\"退课成功!\");";
script += " window.opener.location.reload();";
script += " window.close();</script>";
Response.Write(""+script+"");
}
catch
{
Response.Write("<script>alert(\"请先注册\")</script>");
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
呵呵,你这个还是asp的方式
还想问问你从什么地方看出我的还是asp方式,那怎么才能把它改成asp.net方式的呢,谢谢了
if(!IsPostBack)
{
......
}
中
asp.net方式:按钮有各自的处理事件直接处理