GridView 常用事件--删除,更新,删除,取消等

要设置的基本属性:DataKeyNames

前台代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:GridView ID="GridView1" runat="server" CellPadding="4" AutoGenerateColumns="False"  OnRowCommand="GridViewID_RowCommand" DataKeyNames="id,name" OnRowCancelingEdit="GridViewID_RowCancelingEdit" OnRowEditing="GridViewID_RowEditing" OnRowUpdating="GridViewID_RowUpdating" >
<Columns>
     
<asp:TemplateField HeaderText="编号">
          
<ItemTemplate>
               
<asp:Label ID="lbid" runat="server" Text='<%#Eval("id") %>'></asp:Label>
          
</ItemTemplate>
     
</asp:TemplateField>
     
<asp:TemplateField HeaderText="课本版本">
     
<EditItemTemplate>
          
<asp:TextBox ID="TextBox" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
     
</EditItemTemplate>

     
<ItemTemplate>
           
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
      
</ItemTemplate>
      
</asp:TemplateField>
      
<asp:CommandField HeaderText="编辑" ShowEditButton="True" >
      
</asp:CommandField>
      
<asp:ButtonField HeaderText="管理" Text="&lt;div id=&quot;de&quot; οnclick=&quot;JavaScript:return confirm('确认要删除本条记录?')&quot;&gt;删除&lt;/div&gt;" CommandName="version_delete" >
     
</asp:ButtonField>
     
</Columns>
</asp:GridView>

数据绑定:

ContractedBlock.gif ExpandedBlockStart.gif Code
protected void MyDataBind()
{
    DataTable Myddt
=New DataTable;
    version_op v_op 
= new bookversion_op();
    Myddt 
= version_op.version_selete();
    
this.GridViewID.DataSource = Myddt;
    
this.GridViewID.DataBind();
}

 后台代码:

取消操作:

None.gif protected void GridViewID_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
None.gif    {
None.gif        GridViewID.EditIndex = -1;
None.gif        this.MyDatabind();
None.gif    }

 

更新操作:

ContractedBlock.gif ExpandedBlockStart.gif 更新操作
protected void GridViewID_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
// int _version_id = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim());//BoundField中便用,但编号也会出现TextBox
        
//如果是模版列一定要用FindControl
        int id = int.Parse(((Label)GridViewID.Rows[e.RowIndex].Cells[0].FindControl("lbid")).Text.ToString().Trim());
        
string  name = ((TextBox)GridViewID.Rows[e.RowIndex].Cells[1].FindControl("TextBox")).Text.ToString().Trim();
        
try
        {
            
int result = version_op.version_update(id, name);
            GridViewID.EditIndex 
= -1;
            
this.MyDatabind();
        }
        
catch (Exception wwdex)
        {
            Tools.MSBox(
"数据库操作失败:" + wwdex.ToString());
        }
    }

删除操作:

//用ButtonField要设置CommandName属性

设计器中Text:<div id="de" οnclick="JavaScript:return confirm('确认要删除本条记录?')">删除</div>

(Text)代码请看上前台代码

ContractedBlock.gif ExpandedBlockStart.gif Code
protected void GridViewID_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
if (e.CommandName.Equals("version_delete"))
        {
            
int id = Convert.ToInt32(this.GridViewID.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);
            
int result = 0;
            
try
            {
                result 
= version_op.version_delete(id);
            }
            
catch (Exception ex)
            {
                Tools.MSBox(
"数据库操作失败:" + ex.ToString());
            }
            
if (result == 1)
            {
                Response.Write(
"<script language='javascript'>alert('删除成功!')</script>");
                MyDatabind();
            }
            
else
            {
                Response.Write(
"<script language='javascript'>alert('数据库操作失败!result=" + result + "')</script>");
                
return;
            }

编辑操作:

None.gif protected   void  GridViewID_RowEditing( object  sender, GridViewEditEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
this.GridViewID.EditIndex = e.NewEditIndex;
InBlock.gif        
this.GridViewID.EditRowStyle.BackColor = Color.AliceBlue;//设置编辑状态下的样式,要引入System.Drawing命名空间
InBlock.gif
        this.MyDatabind();
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/aito/archive/2008/08/25/1276245.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值