想不通了
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
oleDbConnection1.Open();
if(!IsPostBack)
{
  string sql = "select * from classes order by classes";
  OleDbCommand oleDbSelectCommand1 = new OleDbCommand(sql,oleDbConnection1);
  System.Data.OleDb.OleDbDataReader dr = oleDbSelectCommand1.ExecuteReader();
  //DropDownList1.DataSource = ds.Tables[0].DefaultView;
  DropDownList1.DataTextField = "classes";
  DropDownList1.DataValueField = "id";
  DropDownList1.DataSource = dr;
  DropDownList1.DataBind();
  DropDownList1.Items.Insert(0,new ListItem("请选择班级","0"));
      dr.Close();
} //绑定DataList1
string sqlstr = "select testname.testname as testname,testname.testid as testid,classes.classes as classes,classes.id as classid,testname.id as id from testname,classes where testname.endsinal=0 and testname.teacher='"+Session["username"]+"' and testname.testclass=classes.id order by createdate desc";
OleDbDataAdapter oda = new OleDbDataAdapter(sqlstr,oleDbConnection1);

oda.Fill(ds,"testname");
DataList1.DataSource = ds.Tables[0].DefaultView;
DataList1.DataBind();
  
//绑定DataList2。用于已经结束的考试 。
string sqlstr2 = "select testname.testname as testname,testname.testid as testid,classes.classes as classes,classes.id as classid,testname.id as id from testname,classes where testname.endsinal=1 and testname.teacher='"+Session["username"]+"' and testname.testclass=classes.id order by createdate desc";
OleDbDataAdapter oda2 = new OleDbDataAdapter(sqlstr2,oleDbConnection1);
oda2.Fill(ds,"testname2");
DataList2.DataSource = ds.Tables[1].DefaultView;
DataList2.DataBind();
oleDbConnection1.Close();
}这个按钮的事件private void Button1_Click(object sender, System.EventArgs e)
{
if(Session.Contents["username"] == null)
{
Response.Redirect("login.aspx");
}
else
{
string sqlstr4 = "select max(testid) from testname";
//OleDbCommand sqlmax = new OleDbCommand(sqlstr4,oleDbConnection1);
OleDbDataAdapter oda3 = new OleDbDataAdapter(sqlstr4,oleDbConnection1);
DataSet dt1 = new DataSet();
oda3.Fill(dt1,"table_maxid");
int maxid;
if(dt1.Tables[0].Rows[0][0] == null)
{
           maxid = 1;
    }
                else
                {
                  maxid = (int)dt1.Tables[0].Rows[0][0]+1;
                }
string sqlstr3 = "Proc_Addtest '"+testname.Text+"','"+testpwd.Text+"','"+Session.Contents["username"]+"',"+int.Parse(DropDownList1.SelectedItem.Value.ToString().Trim());
OleDbCommand sqlcmd = new OleDbCommand(sqlstr3,oleDbConnection1);
//Response.Write(sqlstr3);
//Response.End();
oleDbConnection1.Open();
                sqlcmd.ExecuteNonQuery();
oleDbConnection1.Close();
}
}

解决方案 »

  1.   

    Page_Load事件中的代码要写在
    if(!this.IsPostBack)
    {
        ...
    }
      

  2.   

    绑定DataList1、DataList2的部分如果放在里面的话,如果添加了数据,按刷新都不会重新绑定了,难道又要加一个服务器按钮吗?
      

  3.   

    回happyfamily(FreeHorse),如果操作者还要继续录入记录呢?
      

  4.   

    to:webabc(网博)
    请问具体怎么操作呢?
    用这个吗?
    this.Load +=new EventHandler(this.Page_Load);如果是的话,还是不行啊