利用GridView进行编辑操作

     在Visual Studio 2005中,GridView的使用频率是比较的大,我们可以里用它提供的编辑、删除功能,进行编辑、删除操作,在进行编辑操作时,我们希望我们相对应的表的主键值保持不变, 我们可以将某一列,转化为模板列,对木板列进行编辑操作。
     HTML代码如下:

<% @ Page Language="C#" AutoEventWireup="true" StylesheetTheme="Default" Theme="Default"
    CodeFile
="OfficeType.aspx.cs" Inherits="Admin_OfficeType" 
%>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
    
< title > 办公用品类型 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
        
< fieldset >
            
< legend > 相关设置 </ legend >
            
< div  id ="tabsF" >
                
< ul >
                    
< li >< href ="Default.aspx"  title ="返回" >< span > 返回 </ span ></ a ></ li >
                
</ ul >
            
</ div >
        
</ fieldset >
        
< div >
            
< fieldset >
                
< legend > 办公用品类型列表: </ legend >
                
< asp:GridView  ID ="gvOfficeType"  runat ="server"  SkinID ="Default_GridView"  OnRowCancelingEdit ="gvOfficeType_RowCancelingEdit"
                    OnRowEditing
="gvOfficeType_RowEditing"  OnRowUpdating ="gvOfficeType_RowUpdating"
                    OnRowDataBound
="gvOfficeType_RowDataBound" >
                    
< Columns >
                        
< asp:BoundField  DataField ="TypeName"  HeaderText ="类型名称"   />
                        
< asp:TemplateField  HeaderText ="类型编号" >
                            
< EditItemTemplate >
                                
< asp:TextBox  ID ="TextBox1"  runat ="server"  Enabled ="False"  Text ='<%#  Bind("TypeCode") % > '> </ asp:TextBox >
                            
</ EditItemTemplate >
                            
< ItemTemplate >
                                
< asp:Label  ID ="Label1"  runat ="server"  Text ='<%#  Bind("TypeCode") % > '> </ asp:Label >
                            
</ ItemTemplate >
                        
</ asp:TemplateField >
                        
< asp:TemplateField  HeaderText ="是否启用" >
                            
< EditItemTemplate >
                                
< asp:CheckBox  ID ="CheckBox1"  runat ="server"  Checked ='<%#  Bind("IfUsing") % > ' Text="启用" />
                            
</ EditItemTemplate >
                            
< ItemTemplate >
                                
< asp:CheckBox  ID ="CheckBox1"  runat ="server"  Checked ='<%#  Bind("IfUsing") % > ' Enabled="false"
                                    Text="启用" />
                            
</ ItemTemplate >
                        
</ asp:TemplateField >
                        
< asp:CommandField  ShowEditButton ="True"  CausesValidation ="False"   />
                    
</ Columns >
                
</ asp:GridView >
            
</ fieldset >
            
< fieldset  style ="text-align: center" >
                
< legend > 创建办公用品类型 </ legend >
                
< table  cellpadding ="4" >
                    
< tr >
                        
< td  class ="tdbg" >
                            类型名称:
                        
</ td >
                        
< td  align ="left" >
                            
< asp:TextBox  ID ="txtTypeName"  runat ="server" ></ asp:TextBox >
                            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator1"  runat ="server"  ControlToValidate ="txtTypeName"
                                ErrorMessage
="类型名称不能为空" > * </ asp:RequiredFieldValidator ></ td >
                    
</ tr >
                    
< tr >
                        
< td  class ="tdbg" >
                            类型编号:
</ td >
                        
< td  align ="left" >
                            
< asp:TextBox  ID ="txtTypeCode"  runat ="server" ></ asp:TextBox >
                            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator2"  runat ="server"  ControlToValidate ="txtTypeCode"
                                ErrorMessage
="类型编号不能为空" > * </ asp:RequiredFieldValidator ></ td >
                    
</ tr >
                    
< tr >
                        
< td  class ="tdbg" >
                            是否启用:
</ td >
                        
< td  align ="left" >
                            
< asp:CheckBox  ID ="chkUsing"  runat ="server"  Checked ="True"  Text ="启用"   /></ td >
                    
</ tr >
                    
< tr >
                        
< td  align ="center"  colspan ="2" >
                            
< asp:Button  ID ="btnSave"  runat ="server"  Text ="保存"  OnClick ="btnSave_Click"   />
                            
< asp:Button  ID ="btnCancel"  CausesValidation ="false"  runat ="server"  Text ="取消"  OnClick ="btnCancel_Click"   />< br  />
                            
< asp:Label  ID ="lblInfo"  runat ="server" ></ asp:Label ></ td >
                    
</ tr >
                
</ table >
            
</ fieldset >
            
< fieldset >
                
< legend > 业务规则 </ legend >
                
< ul >
                    
< li > < span  style ="color: red" > * </ span > 项不能为空 </ li ></ ul >
            
</ fieldset >
        
</ div >
        
< asp:ValidationSummary  ID ="ValidationSummary1"  runat ="server"  ShowMessageBox ="True"
            ShowSummary
="False"   />
    
</ form >
</ body >
</ html >
以下是后台代码:
using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  Admin_OfficeType : System.Web.UI.Page
{
    
//define class
    WebUtility.DataDictionary data;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.gvBind();
        }

    }

    
protected void btnCancel_Click(object sender, EventArgs e)
    
{
        
this.txtTypeCode.Text = "";
        
this.txtTypeName.Text = "";
        
this.lblInfo.Text = "";
    }

    
protected void btnSave_Click(object sender, EventArgs e)
    
{
        data 
= new WebUtility.DataDictionary();
        WebUtility.DataDictionary.OfficeType typeOffice;
        typeOffice.TypeName 
= this.txtTypeName.Text;
        typeOffice.TypeCode 
= this.txtTypeCode.Text;
        
if (this.chkUsing.Checked)
        
{ typeOffice.IfUsing = "1"; }
        
else
        
{ typeOffice.IfUsing = "0"; }
        
if (data.IsOfficeType(typeOffice))
        
{
            
this.lblInfo.ForeColor = System.Drawing.Color.Red;
            
this.lblInfo.Text = this.txtTypeName.Text + " 类型已经存在,请重新填写";
            
return;
        }

        
int i=data.AddOfficeType(typeOffice);
        
if (i > 0)
        
{
            
this.lblInfo.ForeColor = System.Drawing.Color.Green;
            
this.lblInfo.Text = "办公用品类型添加成功";

            
this.txtTypeCode.Text = "";
            
this.txtTypeName.Text = "";
            
this.gvBind();

        }

        
else
        
{
            
this.lblInfo.ForeColor = System.Drawing.Color.Red;
            
this.lblInfo.Text = "办公用品类型添加失败!";

        }

    }

    
private void gvBind()
    
{
        data 
= new WebUtility.DataDictionary();
        
this.gvOfficeType.DataSource = data.DataSetOfficeType(true);
        
this.gvOfficeType.DataBind();
    }

    
protected void gvOfficeType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        
this.gvOfficeType.EditIndex = -1;
        
this.gvBind();
    }

    
protected void gvOfficeType_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        
this.gvOfficeType.EditIndex = e.NewEditIndex;
        
this.gvOfficeType.Rows[e.NewEditIndex].Cells[1].Enabled = false;
        
this.gvBind();
    }

    
protected void gvOfficeType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        TextBox txtName 
= (TextBox)this.gvOfficeType.Rows[e.RowIndex].Cells[0].Controls[0];
        CheckBox chkIfUsing 
= (CheckBox)this.gvOfficeType.Rows[e.RowIndex].Cells[2].FindControl("CheckBox1");
        TextBox txtCode
=(TextBox)this.gvOfficeType.Rows[e.RowIndex].Cells[1].FindControl("TextBox1");
        data 
= new WebUtility.DataDictionary();
        WebUtility.DataDictionary.OfficeType typeOffice;
        typeOffice.TypeName 
= txtName.Text;
        typeOffice.TypeCode 
= txtCode.Text;
        
if (chkIfUsing.Checked)
            typeOffice.IfUsing 
= "1";
        
else
            typeOffice.IfUsing 
= "0";
        
int i=data.UpdateOfficeType(typeOffice);
        
if (i > 0)
        
{
            
this.lblInfo.ForeColor = System.Drawing.Color.Green;
            
this.lblInfo.Text = "办公用品类型更新成功";
        }

        
else
        
{
            
this.lblInfo.ForeColor = System.Drawing.Color.Red;
            
this.lblInfo.Text = "办公用品类型更新失败!";

        }

        
this.gvOfficeType.EditIndex = -1;
        
this.gvBind();
    }

    
protected void gvOfficeType_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"onmouseover""e=this.style.backgroundColor; this.style.backgroundColor='linen'");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=e");
        }

    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值