//添加连接 第一个<appSetting/>后添加<appSetting> <add key="conString" value=""/> </appSetting> 删除原来的<appSetting/> //获取 ConfigurationManager.AppSettings["conString"]; //打开连接 private static void OpenSqlCon() { if(sqlCon.State=Connection.Closed) { sqlCon.Open(); } } //关闭连接 private static void CloseSqlCon() { if(sqlCon.State=Connection.Open) { sqlCon.Close(); } } //执行sql语句返回一个DataSet public static DataSet ExecSql(string sql) { try { OpenSqlCon(); sqlSet.Clear(); sqlDa=new SqlDataAdapter(sql,sqlCon); sqlDa.Fill(sqlSet); CloseSqlCon(); } catch(Exception) { throw(); } finally { CloseSqlCon(); } return sqlSet; } //执行sql语句返回第一个单元格的数据 public static DataSet ExecReturnFirstCell(string sql) { try { OpenSqlCon(); sqlCmd=new SqlCommand(sql,sqlCon); returnString=sqlCmd.ExecutScalar(); CloseSqlCon(); } catch(Exception) { throw(); } finally { CloseSqlCon(); sqlCmd.Dispose(); } return returnString; } //插入数据和更新数据 public static DataSet ExecSql(string sql,string name,string content ) { int i=0; try { OpenSqlCon(); sqlCmd=new SqlCommand(sql,sqlCon); sqlCmd.Paramters.Add('@name',SqlDbType.NChar).value=name; sqlCmd.Paramters.Add('@content',SqlDbType.Text).value=content; i=sqlCmd.ExectuNonQuery(); } catch(Exception) { throw(); } finally { CloseSqlCon(); sqlCmd.Dispose(); } return i; } //删除数据 public static DataSet ExecSql(string sql,string id) { int i=0; try { OpenSqlCon(); sqlCmd=new SqlCommand(sql,sqlCon); sqlCmd.Paramters.Add('@id',SqlDbType.int).value=id; i=sqlCmd.ExectuNonQuery(); } catch(Exception) { throw(); } finally { CloseSqlCon(); sqlCmd.Dispose(); } return i; } //执行存储过程返回一个DataSet //执行存储过程返回第一行第一列的数据 //防止恶意刷新(页面跳转,局部刷新,或者 验证玛.都可以解决这个问题) Response.Redirect(Request.RawUrl ); //取消默认的输入验证,即自动检查提交的文本中的恶意代码(可以提交html代码) 在web.config文件第一个System.web中添加 <pages validateRequest="false"> </Pages> <%#Server.HtmlEncode(Eval("content").ToString())%> //注释 #region #endregion //分页 PagedDataSource ps = new PagedDataSource(); ps.DataSource = dt.DefaultView ; ps.AllowPaging=true; ps.PageSize=5; ps.CurrentPageIndex = ViewState["Page"]; ViewState["count"]=ps.PageCount; 首页:ViewState["page"]=0;DataSource=ps;DataBind(); 下一页:ViewState["page"]=int.Parse(ViewState["page"].ToString())-1;DataSource=ps;DataBind(); 上一页:ViewState["page"]=int.Parse(ViewState["page"].ToString())+1;DataSource=ps;DataBind(); 尾页: ViewState["page"] = int.Parse(ViewState["count"].ToString()) - 1;DataSource=ps;DataBind(); LabSum.Text = ps.DataSourceCount.ToString ();//总数据条数 //输出提示信息 Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('对不起!您还没登录,请先登录!');window.location='Default.aspx'</script>"); //获得时间 <script type="text/javascript"> function showTime() { var now = new Date(); var month=now.getMonth()+1; document.getElementById("labTime").innerHTML ="今天是:" +now.getFullYear ()+"-"+month+"-"+now.getDate () +" " +now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(); window.setTimeout("showTime()",1000); } </script> //DataList RepeatDirection="Horizontal" 数据横向显示 Vertical数据竖向显示 RepeatColumns="2" //OnItemCommand CommandArgument='<%# DataBinder.Eval(Container.DataItem, "商品编号") %>' CommandName="buy" protected void dLRecommend_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "detailSee") { AddressBack(e); } else if (e.CommandName == "buy") { AddShopCart(e); } } //查找控件 Textbox t e.Item.FindControl("id") as Textbox //DataView的RowFilter属性实现SQL语句SELECT语句所实现的功能 DataTable不是数据库中的数据或修改了未保存 Session["table"]=dt; DataTable dt1=(DataTable)Session["table"]; DataView dv=new DataView(dt1); dv.RowFilter="商品名称 like '%"+TextBox1.Text.Trim()+"%'"; DataSource=dv; DataBind(); //删除前确认 OnClientClick= "return confirm('您确定要删除这个附件吗?')" //绑定DrorList 第一种: DataSet ds= new DataSet(); ds = cls2.SelectOle(); DdlSearch1.Items.Clear(); DropDownList1.DataSource = ds.Tables["My"].DefaultView; DropDownList1.DataTextField = "dep_Name"; DropDownList1.DataValueField = "dep_Id"; DropDownList1.DataBind(); 第二种: DataSet set1= new DataSet(); set1 = cls2.SelectOle(); DdlSearch1.Items.Clear(); for (int num1 = 0; num1 < set1.Tables["My"].Rows.Count; num1++) { DropDownList1.Items.Add(set1.Tables["My"].Rows[num1][1].ToString()); } //DrowList插入空项 DropDownList1.Items.Insert(0, new ListItem("", ""));//插入空项,此举必须放到数据绑定之后 //记录滚动条位置 MaintainScrollPositionOnPostback="true" //连接到邮箱 <a href="mailto:wdziliao@yeah.net"> //文本缩进 text-indent: 2em //绑定图片 <img src='<%#Eval("ProductUrl","Ftp/{0}")%>' width="135px" height="100px;" border="0" title ='<%#Eval ("ProductName") %>'> //前台按钮值传到后台 <asp:ImageButton ID="imagebtnRefine" runat="server" CommandName="buy" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "商品编号") %>' ImageUrl="~/images/购买.jpg" /> //手动创建DataTable dtTable = new DataTable(); DataColumn column1 = new DataColumn("序号"); dtTable.Columns.Add(column1); DataRow row; row = dtTable.NewRow(); row["序号"] = key.ToString(); dtTable.Rows.Add(row); //手动为数据控件中的控件添加事件 <asp:TextBox ID="txtNum" runat="server" Text =<%#Eval("Num") %> Width =60px OnTextChanged="txtNum_TextChanged"></asp:TextBox> protected void txtNum_TextChanged(object sender, EventArgs e) { hashCar = (Hashtable)Session["ShopCart"]; //获取其购物车 foreach (GridViewRow gvr in this.gvShopCart.Rows) { TextBox otb = (TextBox)gvr.FindControl("txtNum"); //找到用来输入数量的TextBox控件 int count = Int32.Parse(otb.Text);//获得用户输入的数量值 string BookID = gvr.Cells[1].Text;//得到该商品的ID代 hashCar[BookID] = count;//更新hashTable表 } Session["ShopCart"] = hashCar;//更新购物车 bind(); }