zoukankan      html  css  js  c++  java
  • asp.net 不用GridView自带删除功能,删除一行数据

    数据表一定要有个ID的主键值,你的gridview要设定一下DataKeyNames="ID"这个属性值,接下的事件就好多了,写个OnRowDeleting事件就可以了。

    aspx code
    <asp:GridView ID="GridLog" runat="server" 
    AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" 
    BorderColor="#333" BorderStyle="solid" BorderWidth="1" 
    OnRowDeleting="PublicGridRowDeleting" 
    GridLines="None" Width="98%" ForeColor="#333333"> 
    <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" /> 
    <Columns> 
    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" 
    ReadOnly="True" 
    SortExpression="ID" > 
    <ItemStyle HorizontalAlign="Center" Width="20px" /> 
    </asp:BoundField> 
    <asp:TemplateField HeaderText="删除" ShowHeader="False"> 
    <ItemStyle HorizontalAlign="Center" Width="40px" /> 
    <ItemTemplate> 
    <asp:LinkButton ID="LinkButton1" runat="server" 
    CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删 
    除?');" Text="删除"></asp:LinkButton> 
    </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
    <RowStyle BackColor="#EFF3FB" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" 
    /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" 
    /> 
    <HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" /> 
    <AlternatingRowStyle BackColor="White" /> 
    <EditRowStyle BackColor="#2461BF" /> 
    <EmptyDataTemplate> 
    日志库暂时为空! 
    </EmptyDataTemplate> 
    </asp:GridView> 
    CS Code
    protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
    string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();//strID就是该行的ID 
    string strSQL = "Delete from table " + 
    " WHERE id = " + strID; 
    //执行删除 
    ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT 
    LANGUAGE='javascript'>alert('删除成功!');</script>"); 
    GridBind(); 
    } 

    关键是设定好DataKeyNames后,可以用

    string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();

    string strID= gridView.DataKeys[e.RowIndex]["ID"].ToString();  

    //适用于多个字段为主键。设置多个字段主键的,DataKeyNames=各个字段用逗号隔开

    获得选择列的ID值

  • 相关阅读:
    laravel报错1071 Specified key was too long; max key length is 1000 bytes
    【laravel】Eloquent 模型事件和监听方式
    angular使用forRoot() 注册单一实例服务
    js判断电脑是windows系统还是mac系统
    扁平数据根据`parentId`生成树结构
    页面滚动到指定元素区域
    js简洁模式代码
    简单git使用命令
    图片懒加载 echo.js
    页面图片预加载与懒加载
  • 原文地址:https://www.cnblogs.com/ToddLai/p/2755577.html
Copyright © 2011-2022 走看看