gridview insert 插入记录 FooterTemplate 空记录 no record

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

<!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" xml:lang="en" lang="en">
      
<head id="Head1" runat="server">
         
<title>ASP.NET Insert data in Gridview </title>
      
</head>
<body>
   
<form id="form1" runat="server">
      
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
      
<asp:GridView ID="GridView1" ShowFooter="true" runat="server"    
         OnRowCommand
="GridView1_RowCommand1" AutoGenerateColumns="false">
      
<Columns>
         
<asp:TemplateField>
            
<ItemTemplate>
               
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />&nbsp;
               
<asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
            
</ItemTemplate>
            
<EditItemTemplate>
               
<asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />&nbsp;
               
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
            
</EditItemTemplate>
            
<FooterTemplate>
               
<asp:Button Text="Insert" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" />&nbsp;
               
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
            
</FooterTemplate>
         
</asp:TemplateField>
         
<asp:TemplateField >
            
<ItemTemplate>
               
<asp:Label ID="lblValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
            
</ItemTemplate>
            
<EditItemTemplate>
                  
<asp:TextBox ID="tbUpdate" runat="server" Text='<% Bind("Name") %>'></asp:TextBox>
            
</EditItemTemplate>
            
<FooterTemplate>
                  
<asp:TextBox ID="tbInsert" runat="server" Text="" ></asp:TextBox>
            
</FooterTemplate>
            
</asp:TemplateField>
         
</Columns>
         
<EmptyDataTemplate>
               
<asp:TextBox ID="tbEmptyInsert" runat="server"></asp:TextBox><br />
               
<asp:Button ID="btSend" Text="Insert" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />
            
</EmptyDataTemplate>
         
</asp:GridView>
      
</form>
   
</body>
</html>

And the code behind : 

   
14 public partial class Test : System.Web.UI.Page

   
15 {

   
16     protected void Page_Load(object sender, EventArgs e)

   
17     {

   
18         if (!IsPostBack)

   
19         {

   
20             //Create dummy data

   
21             DataTable dt = new DataTable();

   
22             DataColumn dc = new DataColumn("Name");

   
23             dt.Columns.Add(dc);

   
24             DataRow dr = dt.NewRow();

   
25             dr["Name"= "Ivan";

   
26 

   
27             //Uncomment the following line to have data in the grid :)

   
28             //dt.Rows.Add(dr);

   
29 

   
30             //Bind the gridview

   
31             GridView1.DataSource = dt;

   
32             GridView1.DataBind();

   
33         }


   
34         //Recurses through the controls to show the naming of each individual control that is currently in the gridview

   
35         RecurseControls(GridView1.Controls[0].Controls);

   
36         Label1.Text += GridView1.Controls[0].Controls[0].GetType().Name + "<br />";  

   
37     }


   
38 

   
39     void RecurseControls(ControlCollection ctls)

   
40     {

   
41         foreach (Control ctl in ctls)

   
42         {

   
43             if (!ctl.HasControls())

   
44                 Label1.Text += ctl.ClientID + " " + ctl.GetType().Name + "<br />";

   
45             else

   
46                 RecurseControls(ctl.Controls);

   
47         }


   
48     }


   
49 

   
50     protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)

   
51     {

   
52         if (e.CommandName == "EmptyInsert")

   
53         {

   
54             //handle insert here

   
55             TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert"as TextBox;

   
56             Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the emptydatatemplate",tbEmptyInsert.Text);

   
57 

   
58         }


   
59         if (e.CommandName == "Insert")

   
60         {

   
61             //handle insert here

   
62             TextBox tbInsert = GridView1.FooterRow.FindControl("tbInsert"as TextBox;

   
63             Label1.Text = string.Format("You would have inserted the name :  <b>{0}</b> from the footerrow", tbInsert.Text);

   
64         }


   
65     }


   
66 

   
67 }


posted @ 2007-12-30 00:41  Fernando  阅读(2372)  评论(0编辑  收藏  举报