我在Page_Load里生成控件,控件包括单选,多选,按钮,这些我都实现了,还有就是按钮事件我也实现了,但是我在Page_Load里加了if(!IsPostBack)判断,我之前不加if(!IsPostBack)判断的话,每次点按钮做调试的时候都可以进入按钮事件里调试,但是我加了if(!IsPostBack)判断后就进不去按钮事件了,请问这是为什么吗?能给解释下吗?还有就是我要如何解决这个问题?

解决方案 »

  1.   

    你动态生成控件的代码是在if(!IsPostBack)里写的,一旦产生回调之后控件就没加载,所以当然相应不了你的事件了,把你动态生成控件的代码写到if(!IsPostBack)外面去
      

  2.   

    假若写在if(!IsPostBack)里面,页面刷新后根本就没了动态控件了吧?楼主应该贴部分代码出来~
      

  3.   

    假若写在if(!IsPostBack)里面,页面刷新后根本就没了动态控件了吧?楼主应该贴部分代码出来~
      

  4.   

        Label lb=new Label();
        Label lb1 = new Label();
        RadioButtonList rb=new RadioButtonList();
        Button but = new Button();
        CheckBoxList ckb=new CheckBoxList();
        int jump;
        protected void Page_Load(object sender, EventArgs e)
        {
            //读取问题表
            if(!IsPostBack)
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += @"Data Source=" + Server.MapPath("~/App_Data/db.mdb");
                string sql = "select * from responder_20090302 where MemberID='wangyi3360'";
                con = new OleDbConnection(strConnection);
                con.Open();
                com = new OleDbCommand(sql, con);
                OleDbDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    string sql1 = "select * from question where QuestionID1=" + jump + "";
                    con = new OleDbConnection(strConnection);
                    con.Open();
                    com = new OleDbCommand(sql1, con);
                    OleDbDataReader reader = com.ExecuteReader();
                    if (reader.Read())
                    {    //声明label控件
                        lb1.ID = "lb1";
                        Panel1.Controls.Add(lb1);
                        //声明label控件
                        lb.ID = "lb";
                        Panel1.Controls.Add(lb);                    lb1.Text = reader["QuestionID1"].ToString();
                        lb.Text = reader["Question1"].ToString();
                        type = reader["QuestionType1"].ToString();                    if (type == "单选")               //根据问题表内类型来声明控件
                        {
                            //声明单选按钮
                            string sql2 = "select * from answer where QuestionID1=" + lb1.Text + "";
                            con = new OleDbConnection(strConnection);
                            con.Open();
                            com = new OleDbCommand(sql2, con);
                            OleDbDataReader readers = com.ExecuteReader();
                            rb.DataSource = readers;
                            rb.DataTextField = "Answer1";
                            rb.DataBind();
                            Panel1.Controls.Add(rb);
                        }
                        if (type == "多选")
                        {
                            //声明多选按钮
                            string sql3 = "select * from answer where QuestionID1=" + lb1.Text + "";
                            con = new OleDbConnection(strConnection);
                            con.Open();
                            com = new OleDbCommand(sql3, con);
                            OleDbDataReader read1 = com.ExecuteReader();
                            ckb.DataSource = read1;
                            ckb.DataTextField = "Answer1";
                            ckb.DataBind();
                            Panel1.Controls.Add(ckb);
                        }
                        con.Close();
                        but.Text = "下一步";
                        but.Click += new EventHandler(but_Click); //添加相应事件
                        Panel1.Controls.Add(but);
                    }
    这是我的部分代码,请大家帮我看看
      

  5.   

    那1楼对了,控件的动态添加必须在!IsPostbak之外
    笼统来说,你点击按钮刷新的时候动态添加的控件已经消失了,刷新后在Page_Load重新添加,至于它们的值,ViewState给它们保留下来了,所以不必担心值持久的问题~
      

  6.   

    那我的代码问题出现在哪呢?
    我在
    Label lb=new Label(); 
        Label lb1 = new Label(); 
        RadioButtonList rb=new RadioButtonList(); 
        Button but = new Button(); 
        CheckBoxList ckb=new CheckBoxList(); 
        int jump; 
        protected void Page_Load(object sender, EventArgs e)
    声明了控件
      

  7.   

    你可以直接不用if(!IsPostback),用这句代码本身有部分原因就是因为可以不用重复执行Page_load之前执行过的代码,以便节省资源,遇到动态添加控件的情况,就要适当减少使用这句代码~
      

  8.   

    现在是我不用if(!IsPostBack)了,为什么点动态生成的按钮,调试的时候也进不去事件了呢?
      

  9.   

    只能在if(!IsPostBack)
    一个页面的加载过程首先就是对页面的控件(以及页面本身)初始化。
    当页面回调时页面的将Web的所有服务器控件重新初始化一次
    如果在if(!IsPostBack)的话就不能生成
      

  10.   

    你最好先弄清楚ASP.NET页面生命周期