ASP.NET在DataGrid快速添加新行

來源:互聯網
上載者:User
asp.net|datagrid    ASP.net DataGrid為我們提供的內建的記錄行編輯功能,但是沒有提供內建的添加新行的功能。一個辦法就是:在DataTable中添加新行,然後再重新綁定到DataGrid,這個辦法可行,但在更新前需要進行確認,可能會產生空行。另外一個解決辦法就是:利用DataGrid footer template來提供一個空的行,這樣既可以提高速度,也可以避免其它方法帶來的不足。
  
  為了為瀏覽者提供一個空行,我們使用DataGrid的Footer Template,我們直接在Footer Template裡添加文字框,這樣可以避免不必要的操作:比如點擊“編輯”按鈕等。這樣也可以減少往複資料提交的次數。我們這裡仍然LinkButton(插入),並設定CommandName屬性為“Insert”,這個CommandName在DataGrid的ItemCommand事件中,確保只有使用者點擊了“Insert”LinkButton才添加記錄。添加到資料庫的方法是很簡單的。
  
  下面的這個例子提供了DataGrid快速添加新行的功能。aspx代碼和Cohe Behind代碼分別如下,注意更改資料錄連接字串:
  
  查看例子
  
  InsertableDataGrid.aspx
  
  <%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" AutoEventWireup="false" Codebehind="InsertableDataGrid.aspx.vb" Inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">Web</a>.InserTableDataGrid"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <HTML>
  <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
  <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
  <meta name="vs_defaultClientScript" content="<a href="http://dev.21tx.com/web/javascript/" target="_blank">JavaScript</a>">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None"
   BorderWidth="1px" BackColor="White" CellPadding="4" ShowFooter="True" AutoGenerateColumns="False">
   <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
   <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
   <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
   <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
   <Columns>
   <asp:TemplateColumn HeaderText="Employee ID">
   <ItemTemplate>
   <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
   </asp:Label>
   </ItemTemplate>
   <FooterTemplate>
   <asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
   </FooterTemplate>
   <EditItemTemplate>
   <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
   </asp:TextBox>
   </EditItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="Last Name">
   <ItemTemplate>
   <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>'>
   </asp:Label>
   </ItemTemplate>
   <FooterTemplate>
   <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
   </FooterTemplate>
   <EditItemTemplate>
   <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
   </EditItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="First Name">
   <ItemTemplate>
   <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstname") %>'>
   </asp:Label>
   </ItemTemplate>
   <FooterTemplate>
   <asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
   </FooterTemplate>
   <EditItemTemplate>
   <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
   </EditItemTemplate>
   </asp:TemplateColumn>
   </Columns>
   <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
   </asp:DataGrid>
  </form>
  </body>
  </HTML>
  
  InsertableDataGrid.aspx.vb
  
  Imports System.Data
  Imports System.Data.SqlClient
  
  Public Class InserTableDataGrid
   Inherits System.Web.UI.Page
   Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
  
  #Region " Web Form Designer Generated Code "
  
   'This call is required by the Web Form Designer.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  
   End Sub
  
   Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
   End Sub
  
  #End Region
  
   Dim connstr As String = "Integrated Security=SSPI;User ID=sa;Initial Catalog=NorthWind;Data Source=.\netsdk"
  
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   If Not Page.IsPostBack Then
   BindGrid()
   End If
   End Sub
  
   Sub BindGrid()
   Dim cnn As New SqlConnection(connstr)
   Dim da As New SqlDataAdapter("select employeeid,lastname,firstname from employees", cnn)
   Dim ds As New DataSet()
   da.Fill(ds, "employees")
  
   DataGrid1.DataSource = ds
   DataGrid1.DataBind()
   End Sub
   Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)_
   Handles DataGrid1.ItemCommand
   If e.CommandName = "Insert" Then
   Dim cnn As New SqlConnection(connstr)
   Dim t1 As TextBox = e.Item.FindControl("textbox2")
   Dim t2 As TextBox = e.Item.FindControl("textbox4")
   cnn.Open()
   Dim cmd As New SqlCommand("insert into employees(lastname,firstname) values('" & t1.Text & "','" & t2.Text & "')", cnn)
   cmd.ExecuteNonQuery()
   cnn.Close()
   BindGrid()
   End If
   End Sub
  End Class

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.