手工创建datagrid数据列/模板列/按钮事件+简单的数据操作类(asp.net)
2008-04-16 10:38
381 查看
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)
...{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
...{
initData();
}
string strconn="server=;user id=sa;password=;database=clothing";
private void initData()
...{
string strSql="select * from tblProduct";
DataSet ds=new DataSet();
ds=SqlAccess.getDataSet(this.strconn,strSql);
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGridCols.CreateCols(this.DataGrid1,"Pro_ID","叙号",50);
DataGridCols.CreateCols(this.DataGrid1,"Pro_Code","编号",50);
DataGridCols.CreateCols(this.DataGrid1,"Pro_Name","名称",100);
DataGridCols.CreateTemplateCol(this.DataGrid1,"Type","类型");
DataGridCols.CreateButton(this.DataGrid1,"del","删除");
this.DataGrid1.DataBind();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
...{
string strSql="select * from tblProduct_Type";
DataSet ds=new DataSet();
ds=SqlAccess.getDataSet(this.strconn,strSql);
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
...{
DropDownList ddl=(DropDownList)e.Item.FindControl("Type");
ddl.DataSource=ds.Tables[0];
ddl.DataTextField="Type_Name";
ddl.DataValueField="Type_ID";
ddl.DataBind();
ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"Type_ID"))).Selected=true;
}
}
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
...{
if(e.CommandName=="del")
...{
SqlConnection conn=new SqlConnection(this.strconn);
SqlCommand comm=new SqlCommand("delete tblProduct where Pro_ID=@id",conn);
SqlParameter parm1=new SqlParameter("@id",SqlDbType.Int);
parm1.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
this.initData();
}
}
}
}
相关文章推荐
- 手工创建datagrid数据列/模板列/按钮事件+简单的数据操作类(asp.net)
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- [转]ASP.NET一个页面多个Button按钮事件避免数据验证控件冲突方法
- asp.net简单新闻系统之基础数据操作类
- ASP.NET一个页面多个Button按钮事件避免数据验证控件冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- asp.net C#动态添加创建生成button按钮控件没有触发执行响应click绑定事件处理
- (转)ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET使用表单验证在注销后使得浏览器后退按钮失效的简单方法
- ASP.NET 点击前台服务器按钮后, 刷新. 重新执行 按钮事件
- ASP.NET 2.0数据教程之一: 创建一个数据访问层
- Asp.Net Grieview Eval 绑定数据 调用JS事件
- asp.net中数据链路层的创建(3)
- asp.net 回车激发指定按钮事件
- ASP.NET的按钮不触发事件的问题解决办法
- asp.net中Button按钮点击两次才执行Onclick事件的原因
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之数据篇