zoukankan      html  css  js  c++  java
  • 新增及删除Grid行

    示例效果如下

    点击 新增 按钮 Grid将增加一空行 用于输入
    点击 删除 按钮 将删除该行记录并重新绑定Grid

    相关代码如下
    HTML部分

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PrivateWork_Default" %>

    <!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>Grid 新增及删除一行</title>
    </head>
    <body>
        
    <form id="form1" runat="server">

            
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
                
    <Columns>
                    
    <asp:TemplateField HeaderText="pID">
                        
    <ItemTemplate>
                            
    <asp:TextBox ID="txt_pID" runat="server" Width="98" 
                                  Text
    ='<%# Bind("pID") %>'></asp:TextBox>
                        
    </ItemTemplate>
                        
    <HeaderStyle HorizontalAlign="Center" />
                    
    </asp:TemplateField>
                    
    <asp:TemplateField HeaderText="pName">
                        
    <ItemTemplate>
                            
    <asp:TextBox ID="txt_pName" runat="server" Width="98" 
                                  Text
    ='<%# Bind("pName") %>' ></asp:TextBox>
                        
    </ItemTemplate>
                        
    <HeaderStyle HorizontalAlign="Center" />
                    
    </asp:TemplateField>
                    
    <asp:TemplateField>
                        
    <HeaderTemplate>
                            
    <asp:Button ID="btn_NewAdd" runat="server" Text="新增" 
                                OnClick
    ="btn_NewAdd_Click" />
                        
    </HeaderTemplate>
                        
    <ItemTemplate>
                            
    <asp:Button ID="btn_Delete" runat="server" Text="删除" 
                             CommandArgument
    ="<%# GridView1.Rows.Count %>" 
                             OnClick
    ="btn_Delete_Click" />
                        
    </ItemTemplate>
                    
    </asp:TemplateField>
                
    </Columns>
           
    </asp:GridView>

            
    <asp:Button ID="btn_SaveAndShow" runat="server" Text="保存并显示数据在GridView2" 
              OnClick
    ="btn_SaveAndShow_Click" /><br />
            
    <asp:GridView ID="GridView2" runat="server" Width="200">
            
    </asp:GridView>
            
        
    </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 PrivateWork_Default : System.Web.UI.Page
    {
        
    Page_Load 构造DataTable

        
    Grid 新增一行

        
    Grid 删除一行

        
    保存并显示 按钮
    }


     

  • 相关阅读:
    BZOJ4416 [Shoi2013]阶乘字符串 【序列自动机 + 状压dp】
    BZOJ2159 Crash 的文明世界 【第二类斯特林数 + 树形dp】
    快速求原根
    BZOJ2530 [Poi2011]Party 【贪心】
    BZOJ2213 [Poi2011]Difference 【乱搞】
    BZOJ2276 [Poi2011]Temperature 【单调队列】
    多项式除法
    loj2538 「PKUWC2018」Slay the Spire 【dp】
    loj2537 「PKUWC2018」Minimax 【概率 + 线段树合并】
    Java多线程之线程的暂停
  • 原文地址:https://www.cnblogs.com/freeliver54/p/919835.html
Copyright © 2011-2022 走看看