在GridView加入了一”新增按扭“,及InsertItemTemplate,如何在,点击新增,就在当前行下新增一行,并显示InsertItemTemplate的内容,然后,点击更新,即可保存所新增的内容?

解决方案 »

  1.   

    这个功能,好像不能直接在gridview上操作.
    你可以用代码连接数据库,再插入记录.
    问题是,你的commandName="Edit"会出现edit模板.
    但是却没有insert模板.
    或许是我不知道,网上也没有找到.
      

  2.   

    aspx
    <%@ 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>
    aspx.cs
     public partial class Test : System.Web.UI.Page
     {     protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
            {            //Create dummy data             DataTable dt = new DataTable();
                 DataColumn dc = new DataColumn("Name");
                 dt.Columns.Add(dc);
                DataRow dr = dt.NewRow();
                 dr["Name"] = "Ivan";
                 //Uncomment the following line to have data in the grid :)
               //dt.Rows.Add(dr);
        //Bind the gridview
                GridView1.DataSource = dt;             GridView1.DataBind();
            }
             //Recurses through the controls to show the naming of each individual control that is currently in the gridview
            RecurseControls(GridView1.Controls[0].Controls);
          Label1.Text += GridView1.Controls[0].Controls[0].GetType().Name + "<br />";  
         }    void RecurseControls(ControlCollection ctls)
         {
             foreach (Control ctl in ctls)
             {
                 if (!ctl.HasControls())
                    Label1.Text += ctl.ClientID + " " + ctl.GetType().Name + "<br />";
                else
                    RecurseControls(ctl.Controls);
            }
         }
     
        protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
         {
             if (e.CommandName == "EmptyInsert")
             {
                 //handle insert here
                 TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert") as TextBox;
                 Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the emptydatatemplate",tbEmptyInsert.Text);        }
             if (e.CommandName == "Insert")
             {
                 //handle insert here
                TextBox tbInsert = GridView1.FooterRow.FindControl("tbInsert") as TextBox;
                 Label1.Text = string.Format("You would have inserted the name :  <b>{0}</b> from the footerrow", tbInsert.Text);
             }
        }
     }
      

  3.   

    要自己写代码更新,如果想偷懒,用sqldatasource简单
      

  4.   

    不支持insert,必须插入后重新绑定
      

  5.   

    一般都是在FooterTemplate内加控件(包括一个‘新增’按钮),点击时GridView.FooterRow.FindControl("控件ID"),然后手动执行ObjectDataSource.Insert()方法,并在ObjectDataSource.Inserting方法里把GridView.FooterRow.FindControl("控件ID")找到的值传给ObjectDataSource的InsertParameters。