站内搜索
分类列表
本类阅读排行
本类推荐文章
- ASP技巧:让Len,Left,Right函数识...
- 用ASP实现IE地址栏参数的判断
- 如何实现ASP.NET网站个性化
- 如何在Asp.net中使用HtmlArea编辑...
- 用ASP.Net实现在线压缩和解压缩
- ASP实现不存在的网页就自动发送邮...
- 用ASP取出HTML里面的图片地址的函...
- asp.net跳转页面的三种方法比较
- 一个Winsock组件
- 用WinSock设计Chat程序(转)
- 在MFC应用中深入定制WebBrowser控...
- 这几天有人问关于编应.dll的问题,...
- 在Visual J++中编写ASP COM组件(转...
- 一个显示Grid的VBScript对象
- 好东西,快来看呀:(转载自中华网...
广告
dataGrid 中添加数据
作者: 来源: 点击: 日期:2007-2-17 23:27:20
文件代码:
test.aspx
===========================>
<%@ Page Language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
const string DataTableName="Employees";
SqlConnection conn;
SqlDataAdapter adapter;
void Page_Load(Object src, EventArgs e)
{
conn=new SqlConnection("server=(local);database=pubs;uid=sa;pwd=");
adapter=new SqlDataAdapter("select * from employees",conn);
if(!Page.IsPostBack){
BindData();
}
}
//绑定数据
void BindData(){
//先从Session中获取DataTable
DataTable table=(DataTable)Session[DataTableName];
//若Session中的DataTable不存在,则从数据库获取数据
if(table==null){
table=new DataTable();
adapter.Fill(table);
//将DataTable保存到Session中
SaveTableToSession(table);
table.Columns["id"].AutoIncrement=true;
}
grid.DataSource=table;
grid.DataBind();
}
void ChangePage(object src,DataGridPageChangedEventArgs e){
grid.CurrentPageIndex=e.NewPageIndex;
BindData();
}
void UpdateDataTable(object src,EventArgs e){
try{
DataTable table=GetTableFromSession();
string name;
byte age;
string address;
CheckBox ckdel;
for(int i=0;i<grid.Items.Count;i++){
DataGridItem dgitem=grid.Items[i];
int empId=(int)grid.DataKeys[dgitem.ItemIndex];
ckdel=dgitem.FindControl("delckb") as CheckBox;
name=((TextBox)dgitem.Cells[0].Controls[1]).Text;
age=byte.Parse(((TextBox)dgitem.Cells[1].Controls[1]).Text);
address=((TextBox)dgitem.Cells[2].Controls[1]).Text;
UpdateEmployee(table,empId,name,age,address,ckdel.Checked);
}
SaveTableToSession(table);
cancelbtn.Enabled=true;
int rowcount=0;
foreach(DataRow row in table.Rows){
if(row.RowState!=DataRowState.Deleted)
rowcount++;
}
if(Math.Ceiling(rowcount/5.0)==grid.CurrentPageIndex&&grid.CurrentPageIndex>0)
grid.CurrentPageIndex-=1;
BindData();
msglbl.Text="更新数据表成功!";
}
catch(Exception ex){
msglbl.Text="更新数据表失败,出现意外错误:"+ex.Message;
}
}
void UpdateEmployee(DataTable table,int id,string name,byte age,string address,bool isDelete){
for(int i=0;i<table.Rows.Count;i++){
test.aspx
===========================>
<%@ Page Language="C#" Debug="True"%>
<%@Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
const string DataTableName="Employees";
SqlConnection conn;
SqlDataAdapter adapter;
void Page_Load(Object src, EventArgs e)
{
conn=new SqlConnection("server=(local);database=pubs;uid=sa;pwd=");
adapter=new SqlDataAdapter("select * from employees",conn);
if(!Page.IsPostBack){
BindData();
}
}
//绑定数据
void BindData(){
//先从Session中获取DataTable
DataTable table=(DataTable)Session[DataTableName];
//若Session中的DataTable不存在,则从数据库获取数据
if(table==null){
table=new DataTable();
adapter.Fill(table);
//将DataTable保存到Session中
SaveTableToSession(table);
table.Columns["id"].AutoIncrement=true;
}
grid.DataSource=table;
grid.DataBind();
}
void ChangePage(object src,DataGridPageChangedEventArgs e){
grid.CurrentPageIndex=e.NewPageIndex;
BindData();
}
void UpdateDataTable(object src,EventArgs e){
try{
DataTable table=GetTableFromSession();
string name;
byte age;
string address;
CheckBox ckdel;
for(int i=0;i<grid.Items.Count;i++){
DataGridItem dgitem=grid.Items[i];
int empId=(int)grid.DataKeys[dgitem.ItemIndex];
ckdel=dgitem.FindControl("delckb") as CheckBox;
name=((TextBox)dgitem.Cells[0].Controls[1]).Text;
age=byte.Parse(((TextBox)dgitem.Cells[1].Controls[1]).Text);
address=((TextBox)dgitem.Cells[2].Controls[1]).Text;
UpdateEmployee(table,empId,name,age,address,ckdel.Checked);
}
SaveTableToSession(table);
cancelbtn.Enabled=true;
int rowcount=0;
foreach(DataRow row in table.Rows){
if(row.RowState!=DataRowState.Deleted)
rowcount++;
}
if(Math.Ceiling(rowcount/5.0)==grid.CurrentPageIndex&&grid.CurrentPageIndex>0)
grid.CurrentPageIndex-=1;
BindData();
msglbl.Text="更新数据表成功!";
}
catch(Exception ex){
msglbl.Text="更新数据表失败,出现意外错误:"+ex.Message;
}
}
void UpdateEmployee(DataTable table,int id,string name,byte age,string address,bool isDelete){
for(int i=0;i<table.Rows.Count;i++){
dataGrid 中添加数据 评论
