控制可编辑行且点击回车执行修改.


ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="RowEditLimit.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.RowEditLimit"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > RowEditLimit </ title >
None.gif        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
ExpandedBlockStart.gifContractedBlock.gif        
< script  language ="javascript" > dot.gif
InBlock.gif    
function ReceiveKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(event.keyCode==13) document.all.Label1.click();
ExpandedSubBlockEnd.gif    }

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

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  Skyendless.DataAccess;
None.gif
None.gif
namespace  DataGridTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// RowEditLimit 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class RowEditLimit : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dg;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
string connectionString = string.Empty;
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
//具体连接字符串,据自身情况而定
InBlock.gif            
//
InBlock.gif
            connectionString = "Server = (local);uid = sa;pwd = sa;DataBase = test";
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CreateDB();
InBlock.gif                DGBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建数据库
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void CreateDB()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
string createDB = @"
InBlock.gif                            if not exists(select 1 from sysobjects where name = 'test' and xtype = 'u')
InBlock.gif                            Begin
InBlock.gif
InBlock.gif                                Create table Test(ID int
InBlock.gif                                ,Content varchar(20)
InBlock.gif                                ,Amount int
InBlock.gif                                ,Total int
InBlock.gif                                )
InBlock.gif
InBlock.gif                                insert into test select 1,'可修改',100,200
InBlock.gif                                union select 2,'固定',200,200
InBlock.gif
InBlock.gif                            End
InBlock.gif                                                
";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,createDB);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Label1.Text = ex.Message.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 绑定数据
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void DGBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//
InBlock.gif                
//具体路径根据自己的情况进行修改
InBlock.gif                
//
InBlock.gif
                string cmdText = "select * from test";
InBlock.gif                ds 
= SqlHelper.ExecuteDataset(connectionString,CommandType.Text,cmdText);
InBlock.gif                dg.DataSource 
= ds.Tables[0].DefaultView;
InBlock.gif                dg.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Label1.Text = ex.Message.ToString ();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.dg.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_ItemCommand);
InBlock.gif            
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(e.Item.Cells[1].Text == "可修改")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    e.Item.Attributes.Add(
"ondblclick","__doPostBack('"+((LinkButton)e.Item.Cells[4].Controls[0]).ClientID.Replace("__","$_")+"','')");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(e.Item.ItemType==ListItemType.EditItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Label1.Attributes.Add("onclick","__doPostBack('"+((LinkButton)e.Item.Cells[5].Controls[0]).ClientID.Replace("__","$_")+"','')");
InBlock.gif                ((TextBox)e.Item.Cells[
2].Controls[0]).Attributes.Add("onmouseover","this.select()");
InBlock.gif                ((TextBox)e.Item.Cells[
3].Controls[0]).Attributes.Add("onmouseover","this.select()");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.CommandName=="edit")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.dg.EditItemIndex=e.Item.ItemIndex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(e.CommandName=="update")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string cmdText = string.Format("Update test set Amount = {0} ,total = {1} where ID = {2}"
InBlock.gif                        ,((TextBox)e.Item.Cells[
2].Controls[0]).Text
InBlock.gif                        ,((TextBox)e.Item.Cells[
3].Controls[0]).Text
InBlock.gif                        ,e.Item.Cells[
0].Text);
InBlock.gif                    
InBlock.gif                    SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,cmdText);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Label1.Text = ex.Message.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.dg.EditItemIndex=-1;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            DGBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/hzuIT/articles/474606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值