仿博客項目整理筆記

js

動態創建div並綁定事件

 var div = document.createElement("div");//創建div存放頁數
    div.className = "tooltip";
    div.innerHTML =i;
    div.name = "t"+i+"";
    div.style.position = "absolute";
    div.style.borderstyle = "solid";
    div.style.border = "red";
    div.style.left = ""+la+"px";
    div.style.width = 100;
    div.onclick = function xxx() {//將div與事件綁定動態
        for (var i = 1; i <= x; i++) {//將table全部隱藏

            var q1 = document.getElementById("t" + i + "");
            q1.style.display = "none";
        }
        oooo = this.name//得到具體哪個按鈕激發的事件
        var q = document.getElementById(oooo);

        q.style.display = "block";
    };
    var son_td = document.createElement("TD");
    son_td.appendChild(div);//將div存放至表格TD中
    son_tr.appendChild(son_td);

<link type="text/css"  rel="Stylesheet" href="css/xwz.css"/>//連接至css
      <script type="text/javascript" src="xjs.js"></script>//連接至js

web後臺

                                Table qqq = new Table();//動態創建table                           

                                TableRow Row = new TableRow();//新建行
                                Row.CssClass = "tbiaoge";//爲新建行提供classname方便css調整位置
                                TableRow Row1 = new TableRow();//新建列
                                Row1.CssClass = "tbiaoge1";                                

                                    TableCell Cell1 = new TableCell();
                                    string xxxxx = reader.GetString(reader.GetOrdinal("tfl"));//得到數據庫中字段爲tfl的值
                                    Cell1.Text = xxxxx;
                                    Row1.Cells.Add(Cell1);//沒次創建兩行,第二行爲回覆信息只需一列。將每列添加到行中
                                    
                                    qqq.Rows.Add(Row);
                                    qqq.Rows.Add(Row1);//將每行添加進table中
                                   
                                    xplgl.Controls.Add(qqq);//將動態創建的表格添加到網頁指定的空間

                                    Button xb = new Button();
                                    xb.Text = "刪除";
                                    xb.CommandName = id.ToString();
                                    xb.BorderStyle = BorderStyle.None;
                                    xb.ForeColor = System.Drawing.Color.Red;//爲動態添加的按鈕字體顏色賦值
                                    xb.Click += new EventHandler(xsc);//動態爲按鈕添加事件

 public void xsc(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {conn.Open();
                 using (SqlCommand cmd = conn.CreateCommand())
                {
                    Button b1 = (Button)sender;//得到具體是哪個按鈕觸法的時間通過按鈕的屬性判斷連接的時間視哪個
                    int x = Convert.ToInt32(b1.CommandName);
                    cmd.CommandText = "delete from xfl where id="+x+"";
                    cmd.ExecuteNonQuery();
                    Response.Redirect("xpl.aspx");//跳轉頁面
                }
            }
       
        }

 

網頁間的傳值

傳值

  Response.Redirect("gerenxiangxi.aspx?id1="啊啊啊啊啊啊"");

接收

string ooo=Request["id1"];

 

配置文件中添加數據庫連接的方法

添加連接

 <connectionStrings>
  <add name="sqlcon1" connectionString="data source=XWZ\實例2008;initial catalog=yhgl;user id=sa;password=123" />
 </connectionStrings>

調用

string constr =ConfigurationManager.ConnectionStrings["sqlcon1"].ConnectionString;

 

發佈了34 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章