none
动态创建的按钮不起作用,急急急! RRS feed

  • 问题

  • 程序的目的:动态建立一个输入框、动态建立一个下拉选择框,动态建立一个按钮,用来输入电话号码,并把电话号码存入数据库中。

    但按钮似乎没有作用,单击按钮后,根本进入不了protected void btnNewTelOK01_Click(object sender, EventArgs e) 中。

    错在哪里?

     

            if (!IsPostBack)
            {
                            ......
                            Label lblNewTel;  //建立一个标签
                            lblNewTel=new Label();
                            lblNewTel.ID = "lblNewTel";
                            lblNewTel.Text = "添加一个新的电话";
                            this.Form.Controls.Add(lblNewTel);

                            TextBox ttbNewTel01 = new TextBox(); //添加一个新的文本框,用来输入电话号码
                            ttbNewTel01.ID = "ttbNewTel01";
                            this.Form.Controls.Add(ttbNewTel01);

                            DropDownList dropNewTelClass01=new DropDownList();  //建立一个下拉菜单,用来显示挑选电话种类,如工作、移动电话、家庭
                            dropNewTelClass01.ID = "dropNewTelClass01";
                            this.Form.Controls.Add(dropNewTelClass01);
                            OleDbCommand commDbabTelClass = new OleDbCommand(strSqlTelClass, connDbab);

                            OleDbDataReader dataReader = commDbabTelClass.ExecuteReader();
                            string varTelClass = "";
                            while (dataReader.Read())
                            {
                                varTelClass = (string)dataReader[0];
                                dropNewTelClass01.Items.Add(varTelClass);
                            }
                            varFromDropdownlist = dropNewTelClass01.SelectedIndex.ToString();

                            Button btnNewTelOK01 = new Button();  //建立一个按钮,用来保存输入的电话号码
                            btnNewTelOK01.ID = "btnNewTelOK01";
                            btnNewTelOK01.Text = "保存";
                            this.Form.Controls.Add(btnNewTelOK01);
                            btnNewTelOK01.Click += new EventHandler(btnNewTelOK01_Click);
          }             

            protected void btnNewTelOK01_Click(object sender, EventArgs e)  //将电话号码保存到数据库
            {

                string strSqlIns = "INSERT INTO " + varTableName + "(TelNum,TelClass) VALUES('" + ((TextBox)this.Form.FindControl("ttbNewTel01")).Text + "','" + ((DropDownList)this.Form.FindControl("dropNewTelClass01")).SelectedValue + "')";
                Response.Write(strSqlIns);
               
                string strConn = "Provider=Microsoft.Jet.OleDb.4.0;";  //连接数据库字符串
                strConn += @"Data source=C:\ChhAtWin\web\AddressBook\AddressBook\App_Data\AddressBookDB.mdb";

                OleDbConnection connDbab = new OleDbConnection(strConn);  //连接数据库
                connDbab.Open();
                OleDbCommand commDbabTelNumTable = new OleDbCommand(strSqlIns, connDbab);
                commDbabTelNumTable.ExecuteNonQuery();
                connDbab.Close();
                
            }


    2011年11月22日 7:27

答案

全部回复

  • 去掉 if (!IsPostBack)判断
    http://blog.csdn.net/zx13525079024
    2011年11月22日 7:53
  • 动态创建 Web 服务器控件的代码应该写在 OnInit 或者 OnPreInit 上,而不是 OnLoad。OnLoad 的话已经太晚了。

    建议阅读 ASP.NET Page Lifecycle。


    Mark Zhou
    2011年11月22日 10:30
  • 我已去掉 if (!IsPostBack)判断,但运行中出现错误信息“表Tel74已存在”,改过的程序为: 

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    namespace AddressBook
    {
        public partial class NewTelNum : System.Web.UI.Page
        {
            string varFromTextbox,varFromDropdownlist,varTableName;

            protected void Page_Load(object sender, EventArgs e)
            {

              //读取数据库        

               string varSex="女";
                string strConn = "Provider=Microsoft.Jet.OleDb.4.0;";  //连接数据库字符串
                strConn += @"Data source=C:\ChhAtWin\web\AddressBook\AddressBook\App_Data\AddressBookDB.mdb";
                int varAddID=int.Parse(Request.QueryString["ID"]);  //接受传递过来的参数

                string strSqlSearch = "select * from Name where addID=" + varAddID;  //选择记录
                string strSqlTelClass = "select TelClassName from TelClass";

                OleDbConnection connDbab = new OleDbConnection(strConn);  //连接数据库
                OleDbCommand commDbabSearch = new OleDbCommand(strSqlSearch, connDbab);  //操作数据库中的表
                connDbab.Open();
                OleDbDataReader dtrdDbabSearch = commDbabSearch.ExecuteReader();
                dtrdDbabSearch.Read();  //读取记录

                //将记录内容放入文本框中
                if (dtrdDbabSearch.HasRows)    //判断DataReader对象中是否包含任何行
                {
                    if((bool)dtrdDbabSearch["Sex"])
                        varSex="男";
                    lblList1.Text = dtrdDbabSearch["LastName"].ToString()+" "+dtrdDbabSearch["FirstName"].ToString()+"    "+varSex+"    "+dtrdDbabSearch["Company"].ToString();

                }
                //判断是否有所需表,若有则打开等待编辑,若无则新建并打开等待编辑
                        varTableName = "Tel"+varAddID.ToString();//文本框中输入的数据库表名称存入varTableName变量中
                    
                        int n=0;
                        string[] restrictions = new string[4];//定义一个数组,其值将决定GetSchema返回的结果
     

                        DataTable userTables = null;//定义一个空表
                        restrictions[3] = "Table";


                        userTables = connDbab.GetSchema("Tables", restrictions);//将数据库所有表的名称信息返回并存放在userTables中

                        DataTable dt = new DataTable();//準備呈現用的DataTable(經過TextBox篩選過的)
                        dt.Columns.Add("TABLE_NAME");//定義欄位
                        foreach (DataRow dr in userTables.Rows)//走訪剛剛從Access撈出來的Tables資訊
                        {
                            if (dr["TABLE_NAME"].ToString().ToLower() == varTableName)//如果Access撈出來的Table名稱等於TextBox輸入的名稱
                            {
                                DataRow dt_r = dt.NewRow();
                                dt_r["TABLE_NAME"] = dr["TABLE_NAME"].ToString();
                                dt.Rows.Add(dt_r);
                                n++;
                            }
                        }


                           
                            if (n == 0)  //如果没有所需的表则创建新表
                            {
                                //Response.Write("<script language=javascript>alert('没有搜索到结果!');</script>");
                               
                                string strSqlCreateTable = "CREATE TABLE " + varTableName + "(TelNum TEXT(50),TelClass TEXT(50))";
                                OleDbCommand commDbabCreateTable = new OleDbCommand(strSqlCreateTable, connDbab);

                                commDbabCreateTable.ExecuteNonQuery();
                                Response.Write("<script language=javascript>alert('成功创建了一个新表!');</script>");
                               
                            }

                            //打开ID号联系人的电话号码表

               
                            Label lblNewTel;
                            lblNewTel=new Label();
                            lblNewTel.ID = "lblNewTel";
                            lblNewTel.Text = "添加一个新的电话";
                            this.Form.Controls.Add(lblNewTel);


                            TextBox ttbNewTel01 = new TextBox(); //添加一个新的文本框,用来输入电话号码
                            ttbNewTel01.ID = "ttbNewTel01";
                            this.Form.Controls.Add(ttbNewTel01);

                            DropDownList dropNewTelClass01=new DropDownList();  //建立一个下拉菜单,用来显示挑选电话种类,如工作、移动电话、家庭
                            dropNewTelClass01.ID = "dropNewTelClass01";
                            this.Form.Controls.Add(dropNewTelClass01);
                            OleDbCommand commDbabTelClass = new OleDbCommand(strSqlTelClass, connDbab);

                            OleDbDataReader dataReader = commDbabTelClass.ExecuteReader();
                            string varTelClass = "";
                            while (dataReader.Read())
                            {
                                varTelClass = (string)dataReader[0];
                                dropNewTelClass01.Items.Add(varTelClass);
                            }
                            varFromDropdownlist = dropNewTelClass01.SelectedIndex.ToString();

                            Button btnNewTelOK01 = new Button();
                            btnNewTelOK01.ID = "btnNewTelOK01";
                            btnNewTelOK01.Text = "保存";

                            this.Form.Controls.Add(btnNewTelOK01);
                            btnNewTelOK01.Click += new EventHandler(btnNewTelOK01_Click);
                       
               
                connDbab.Close();
               
            }

            protected void btnNewTelOK01_Click(object sender, EventArgs e)
            {

                string strSqlIns = "INSERT INTO " + varTableName + "(TelNum,TelClass) VALUES('" + ((TextBox)this.Form.FindControl("ttbNewTel01")).Text + "','" + ((DropDownList)this.Form.FindControl("dropNewTelClass01")).SelectedValue + "')";
                Response.Write(strSqlIns);
               
                string strConn = "Provider=Microsoft.Jet.OleDb.4.0;";  //连接数据库字符串
                strConn += @"Data source=C:\ChhAtWin\web\AddressBook\AddressBook\App_Data\AddressBookDB.mdb";

                OleDbConnection connDbab = new OleDbConnection(strConn);  //连接数据库
                connDbab.Open();
                OleDbCommand commDbabTelNumTable = new OleDbCommand(strSqlIns, connDbab);
                commDbabTelNumTable.ExecuteNonQuery();
                connDbab.Close();
                
            }
        }
    }



    2011年11月23日 6:44
  • 还是不懂,什么是“动态创建 Web 服务器控件的代码应该写在 OnInit 或者 OnPreInit 上”?如何进入阅读 ASP.NET Page Lifecycle
    2011年11月23日 6:48
  • 看下这里

    http://www.qhwins.com/ShowNews/?20-20102313262430308.html


    http://blog.csdn.net/zx13525079024
    2011年11月23日 6:52