网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Java与正则表达式(2年级2).
.浅谈Java中this的使用.
.用LineNumberReader 提供更好的出.
.Struts网站动态导航系统设计思路.
.Java 性能优化技巧集锦 (5).
.Java中Decorate的三种实现方法.
.SQL&Loader可把文件中的数据装载.
.错误的堆大小产生的“堆问题”.
.AOP在大规模软件开发项目中的应用.
.Java语言的中文处理.
.你所需要的《Just Java 2》.
.全面解析Java中的String数据类型.
.Java性能之我见.
.仅用一个类的服务器.
.Java基础:如何成为一名Java初级.
.巧用GC.
.利用ThreadLocal简化struts开发.
.关于游戏中高效的换色方法.
.Java语言基础 五.
.java多线程设计模式详解之一.

datagrid数据列/模板列/按钮事件+操作类

发表日期:2008-1-5



  1)创建datagrid数据列/模板列/按钮的操作类:
  
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  
  namespace WeBTest
  {
  /// <summary>
  /// DataGridColumn 的摘要说明。
  /// </summary>
  public class DataGridCols
  {
  public void DataGridColumn()
  {
  //
  // TODO: 在此处添加构造函数逻辑
  //
  }
  
  public static void CreateCols(System.Web.UI.WebControls.DataGrid DataGrid1,string dataField,string headerText,int i)
  {
  BoundColumn cm=new BoundColumn();
  cm.DataField=dataField;
  cm.HeaderText=headerText;
  cm.HeaderStyle.Width=i;
  DataGrid1.Columns.Add(cm);
  }
  public static void CreateButton(System.Web.UI.WebControls.DataGrid DataGrid1,string commandName,string strText)
  {
  ButtonColumn bc=new ButtonColumn();
  bc.ButtonType=ButtonColumnType.PushButton;
  bc.CommandName=commandName;
  bc.HeaderText="操作";
  bc.Text=strText;
  DataGrid1.Columns.Add(bc);
  }
  
  public static void CreateTemplateCol(System.Web.UI.WebControls.DataGrid DataGrid1,string ID,string headerText)
  {
  TemplateColumn tm=new TemplateColumn();
  tm.ItemTemplate=new DDListCol(ID);
  tm.HeaderText=headerText;
  DataGrid1.Columns.Add(tm);
  }
  }
  }
  
  2)简单的数据库操作类
  
  using System;
  using System.Data;
  using System.Data.SqlClient;
  namespace Webtest
  {
  /// <summary>
  /// SqlAccess 的摘要说明。
  /// </summary>
  public class SqlAccess
  {
  
  // string strConn="server=;user id=sa;passWord=;database=clothing";
  // DataSet ds;
  // SqlDataAdapter da;
  public SqlAccess()
  {
  //
  // TODO: 在此处添加构造函数逻辑
  //
  }
  public static void fillDataSet(string strConnection,string strSql,DataSet ds,string tableName)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  if (ds==null)
  {
  throw new ArgumentNullException( "DataSet" );
  }
  if (tableName==null tableName.Length==0)
  {
  throw new ArgumentNullException( "tableName" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  conn.Open();
  SqlDataAdapter da =new SqlDataAdapter(strSql,conn);
  da.Fill(ds,tableName);
  conn.Close();
  }
  }
  public static void fillDataSet(SqlConnection conn,string strSql,DataSet ds,string tableName)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  if (ds==null)
  {
  throw new ArgumentNullException( "DataSet" );
  }
  if (tableName==null tableName.Length==0)
  {
  throw new ArgumentNullException( "tableName" );
  }
  using(SqlDataAdapter da =new SqlDataAdapter(strSql,conn))
  {
  da.Fill(ds,tableName);
  conn.Close();
  }
  }
  
  public static DataSet getDataSet(string strConnection,string strSql)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  DataSet ds=new DataSet();
  conn.Open();
  SqlDataAdapter da =new SqlDataAdapter(strSql,conn);
  da.Fill(ds);
  conn.Close();
  return ds;
  }
  }
  public static DataSet getDataSet(SqlConnection conn,string strSql)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlDataAdapter da =new SqlDataAdapter(strSql,conn))
  {
  DataSet ds=new DataSet();
  da.Fill(ds);
  conn.Close();
  return ds;
  }
  }
  public static int executeNonQuery(string strConnection,string strSql)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  SqlCommand sqlCmd=new SqlCommand(strSql,conn);
  int i= sqlCmd.ExecuteNonQuery();
  conn.Close();
  return i;
  }
  }
  public static int executeNonQuery(SqlConnection conn,string strSql)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlCommand sqlCmd=new SqlCommand(strSql,conn))
  {
  int i=sqlCmd.ExecuteNonQuery();
  conn.Close();
  return i;
  }
  }
  }
  }
  
  3)创建模板列的类(可以创建n种模板列)
  
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  
  namespace Webtest
  {
  //DropDownList模板列
  public class DDListCol : ITemplate
  {
  string ID;
  public DDListCol(string id)
  {
  this.ID=id;
  }
  public void InstantiateIn(Control container)
  {
  DropDownList dpl = new DropDownList();
  dpl.ID=this.ID ;
  container.Controls.Add(dpl);
  
  }
  }
  //CheckBox模板列
  public class CheckBoxCol : ITemplate
  {
  string ID;
  public CheckBoxCol(string id)
  {
  this.ID=id;
  }
  public void InstantiateIn(Control container)
  {
  CheckBox checkbox = new CheckBox();
  checkbox.ID=this.ID ;
  container.Controls.Add(checkbox);
  }
  }
  }
  
  4)实例:创建数据源和创建datagrid数据列
  
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  using System.Data.SqlClient;
  namespace Webtest
  {
  /// <summary>
  /// WebForm1 的摘要说明。
  /// </summary>
  public class WebForm1 : System.Web.UI.Page
  {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button Button1;
  
  private void Page_Load(object sender, System.EventArgs e)
  {
  // 在此处放置用户代码以初始化页面
  }
  
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
上一篇:谨慎使用单精度/双精度这两数值类型 人气:1050
下一篇:DWR - Direct Web Remoting 实际使用 人气:810
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐