一个GridView编辑删除的例子

<asp:GridView ID="myGvClass" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="id" OnRowCancelingEdit="myGvClass_RowCancelingEdit" OnRowEditing="myGvClass_RowEditing" OnRowUpdating="myGvClass_RowUpdating" OnRowDeleting="myGvClass_RowDeleting" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px">
              
<Columns>
                  
<asp:BoundField DataField="id" HeaderText="序号" ReadOnly="True">
                      
<ItemStyle HorizontalAlign="Center" />
                      
<HeaderStyle HorizontalAlign="Center" Width="10%" /></asp:BoundField>
                  
<asp:BoundField DataField="classname" HeaderText="栏目名称">
                      
<ItemStyle HorizontalAlign="Left" />
                      
<HeaderStyle HorizontalAlign="Left" Width="52%" />
                      
<ControlStyle Width="80%" />
                  
</asp:BoundField>
                  
<asp:BoundField DataField="stime" HeaderText="操作时间" ReadOnly="True">
                      
<ItemStyle HorizontalAlign="Center" />
                      
<HeaderStyle HorizontalAlign="Center" Width="20%" /></asp:BoundField>
                  
<asp:CommandField HeaderText="操作" ShowEditButton="True">
                      
<HeaderStyle HorizontalAlign="Center" Width="12%" />
                      
<ItemStyle HorizontalAlign="Center" /></asp:CommandField>
                  
<asp:TemplateField HeaderText="删除" ShowHeader="False">
                      
<ItemStyle HorizontalAlign="Center" />
                      
<HeaderStyle HorizontalAlign="Center" Width="6%" />
                      
<ItemTemplate>
                          
<asp:LinkButton ID="LinkButton1" OnClientClick="return confirm('您确认删除该记录吗?');" runat="server" CausesValidation="False" CommandName="Delete"
                              Text
="删除"></asp:LinkButton>
                      
</ItemTemplate>
                  
</asp:TemplateField>
              
</Columns>
              
<HeaderStyle BackColor="LightGray" />
              
</asp:GridView>

protected void myGvClass_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        myGvClass.EditIndex 
= e.NewEditIndex;
        
this.myGvClass.EditRowStyle.BackColor = Color.AliceBlue;
        BinData();
    }

    
protected void myGvClass_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        myGvClass.EditIndex 
= -1;
        BinData();
    }

    
protected void myGvClass_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
string strID = this.myGvClass.DataKeys[e.RowIndex].Value.ToString();
        
string strClassName = ((TextBox)(myGvClass.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();

        WebClass.ArticleList tmp 
= new WebClass.ArticleList();
        tmp.EditClass(strClassName,strID);

        myGvClass.EditIndex 
= -1;
        BinData();
    }

    
protected void myGvClass_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
string strID = this.myGvClass.DataKeys[e.RowIndex].Value.ToString();

        WebClass.ArticleList tmp 
= new WebClass.ArticleList();
        tmp.DelClass(strID);

        myGvClass.EditIndex 
= -1;
        BinData();
    }
posted @ 2007-11-21 14:10  胡枫  阅读(613)  评论(0编辑  收藏  举报