![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ 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">
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript">![](/Images/dot.gif)
function ReceiveKey()
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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>
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace DataGridTest
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// RowEditLimit 的摘要说明。
/// </summary>
public class RowEditLimit : System.Web.UI.Page
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//具体连接字符串,据自身情况而定
//
connectionString = "Server = (local);uid = sa;pwd = sa;DataBase = test";
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CreateDB();
DGBind();
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 创建数据库
/// </summary>
private void CreateDB()
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string createDB = @"
if not exists(select 1 from sysobjects where name = 'test' and xtype = 'u')
Begin
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Create table Test(ID int
,Content varchar(20)
,Amount int
,Total int
)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insert into test select 1,'可修改',100,200
union select 2,'固定',200,200
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End
";
try
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,createDB);
}
catch(Exception ex)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString();
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绑定数据
/// </summary>
private void DGBind()
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataSet ds = new DataSet();
try
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//具体路径根据自己的情况进行修改
//
string cmdText = "select * from test";
ds = SqlHelper.ExecuteDataset(connectionString,CommandType.Text,cmdText);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch(Exception ex)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString ();
}
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.Item.Cells[1].Text == "可修改")
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Item.Attributes.Add("ondblclick","__doPostBack('"+((LinkButton)e.Item.Cells[4].Controls[0]).ClientID.Replace("__","$_")+"','')");
}
}
if(e.Item.ItemType==ListItemType.EditItem)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()");
}
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.CommandName=="edit")
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dg.EditItemIndex=e.Item.ItemIndex;
}
if(e.CommandName=="update")
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Label1.Text = ex.Message.ToString();
}
finally
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.dg.EditItemIndex=-1;
}
}
DGBind();
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Button1_Click(object sender, System.EventArgs e)
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
}
![](https://skyendless.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2006-08-11 19:20
石川
阅读(
173)
评论()
编辑
收藏
举报