问高手一个动态生成控件的问题 我在Page_Load里生成控件,控件包括单选,多选,按钮,这些我都实现了,还有就是按钮事件我也实现了,但是我在Page_Load里加了if(!IsPostBack)判断,我之前不加if(!IsPostBack)判断的话,每次点按钮做调试的时候都可以进入按钮事件里调试,但是我加了if(!IsPostBack)判断后就进不去按钮事件了,请问这是为什么吗?能给解释下吗?还有就是我要如何解决这个问题? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你动态生成控件的代码是在if(!IsPostBack)里写的,一旦产生回调之后控件就没加载,所以当然相应不了你的事件了,把你动态生成控件的代码写到if(!IsPostBack)外面去 假若写在if(!IsPostBack)里面,页面刷新后根本就没了动态控件了吧?楼主应该贴部分代码出来~ 假若写在if(!IsPostBack)里面,页面刷新后根本就没了动态控件了吧?楼主应该贴部分代码出来~ 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); }这是我的部分代码,请大家帮我看看 那1楼对了,控件的动态添加必须在!IsPostbak之外笼统来说,你点击按钮刷新的时候动态添加的控件已经消失了,刷新后在Page_Load重新添加,至于它们的值,ViewState给它们保留下来了,所以不必担心值持久的问题~ 那我的代码问题出现在哪呢?我在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),用这句代码本身有部分原因就是因为可以不用重复执行Page_load之前执行过的代码,以便节省资源,遇到动态添加控件的情况,就要适当减少使用这句代码~ 现在是我不用if(!IsPostBack)了,为什么点动态生成的按钮,调试的时候也进不去事件了呢? 只能在if(!IsPostBack)一个页面的加载过程首先就是对页面的控件(以及页面本身)初始化。当页面回调时页面的将Web的所有服务器控件重新初始化一次如果在if(!IsPostBack)的话就不能生成 你最好先弄清楚ASP.NET页面生命周期 防止ASP.NET 后台 被搜索引擎记录 为什么我在前台页面中没法取到值? 小问题,发表的文章\留言不能分段,求救!!! 关于DataGrid中动态添加控件,提交后的问题 下载的Microsoft.Web.UI.WebControls的treeview控件显示有问题?? 请问XmlNode与XmlElement有什么区别 关于用c#在asp.net中和word,excl的交互,急...谢谢 菜鸟高分救一个简单问题!! .Net 下web打印怎么打印计数啊? 请问怎样在上边框实现下边框的打印预览? GridView分页问题 弹出下载文件对话框后,刷新父页面
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);
}
这是我的部分代码,请大家帮我看看
笼统来说,你点击按钮刷新的时候动态添加的控件已经消失了,刷新后在Page_Load重新添加,至于它们的值,ViewState给它们保留下来了,所以不必担心值持久的问题~
我在
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)
声明了控件
一个页面的加载过程首先就是对页面的控件(以及页面本身)初始化。
当页面回调时页面的将Web的所有服务器控件重新初始化一次
如果在if(!IsPostBack)的话就不能生成