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 }


转载于:https://www.cnblogs.com/Fernando/archive/2007/12/30/1020643.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值