
<%
@ Page language="c#" Codebehind="RowEditLimit.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.RowEditLimit" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RowEditLimit</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

<script language="javascript">
function ReceiveKey()

{
if(event.keyCode==13) document.all.Label1.click();
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" onkeydown="ReceiveKey()">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="dg" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 104px" runat="server"
Height="208px" Width="304px" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Content" HeaderText="Content" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Amount" HeaderText="Amount"></asp:BoundColumn>
<asp:BoundColumn DataField="Total" HeaderText="Total"></asp:BoundColumn>
<asp:ButtonColumn Visible="False" Text="edit" CommandName="edit"></asp:ButtonColumn>
<asp:ButtonColumn Visible="False" Text="update" CommandName="update"></asp:ButtonColumn>
</Columns>
</asp:datagrid><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 440px; POSITION: absolute; TOP: 72px" runat="server"
Height="32px" Width="40px">保存</asp:label></FONT></form>
</body>
</HTML>

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 Skyendless.DataAccess;

namespace DataGridTest


{

/**//// <summary>
/// RowEditLimit 的摘要说明。
/// </summary>
public class RowEditLimit : System.Web.UI.Page

{
protected System.Web.UI.WebControls.DataGrid dg;
protected System.Web.UI.WebControls.Label Label1;
string connectionString = string.Empty;
private void Page_Load(object sender, System.EventArgs e)

{
//
//具体连接字符串,据自身情况而定
//
connectionString = "Server = (local);uid = sa;pwd = sa;DataBase = test";
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)

{
CreateDB();
DGBind();
}

}

/**//// <summary>
/// 创建数据库
/// </summary>
private void CreateDB()

{

string createDB = @"
if not exists(select 1 from sysobjects where name = 'test' and xtype = 'u')
Begin

Create table Test(ID int
,Content varchar(20)
,Amount int
,Total int
)

insert into test select 1,'可修改',100,200
union select 2,'固定',200,200

End
";
try

{
SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,createDB);
}
catch(Exception ex)

{
this.Label1.Text = ex.Message.ToString();
}

}

/**//// <summary>
/// 绑定数据
/// </summary>
private void DGBind()

{
DataSet ds = new DataSet();
try

{
//
//具体路径根据自己的情况进行修改
//
string cmdText = "select * from test";
ds = SqlHelper.ExecuteDataset(connectionString,CommandType.Text,cmdText);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch(Exception ex)

{
this.Label1.Text = ex.Message.ToString ();
}
}


Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)

{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.dg.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_ItemCommand);
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

{
if(e.Item.Cells[1].Text == "可修改")

{
e.Item.Attributes.Add("ondblclick","__doPostBack('"+((LinkButton)e.Item.Cells[4].Controls[0]).ClientID.Replace("__","$_")+"','')");
}
}
if(e.Item.ItemType==ListItemType.EditItem)

{
this.Label1.Attributes.Add("onclick","__doPostBack('"+((LinkButton)e.Item.Cells[5].Controls[0]).ClientID.Replace("__","$_")+"','')");
((TextBox)e.Item.Cells[2].Controls[0]).Attributes.Add("onmouseover","this.select()");
((TextBox)e.Item.Cells[3].Controls[0]).Attributes.Add("onmouseover","this.select()");
}
}

private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{
if(e.CommandName=="edit")

{
this.dg.EditItemIndex=e.Item.ItemIndex;
}
if(e.CommandName=="update")

{
try

{
string cmdText = string.Format("Update test set Amount = {0} ,total = {1} where ID = {2}"
,((TextBox)e.Item.Cells[2].Controls[0]).Text
,((TextBox)e.Item.Cells[3].Controls[0]).Text
,e.Item.Cells[0].Text);
SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,cmdText);
}
catch (Exception ex)

{
this.Label1.Text = ex.Message.ToString();
}
finally

{
this.dg.EditItemIndex=-1;
}
}
DGBind();
}

private void Button1_Click(object sender, System.EventArgs e)

{
}
}
}

posted on
2006-08-11 19:20
石川
阅读(
182)
评论()
收藏
举报