在Web中的Datagrid中添加一行
在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:
1<%@ Page Language="vb" AutoEventWireup="false" Codebehind="InsertableDataGrid.aspx.vb" Inherits="aspxWeb.InserTableDataGrid"%>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3<HTML>
4<HEAD>
5<title>WebForm1</title>
6<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
7<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
8<meta name="vs_defaultClientScript" content="JavaScript">
9<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10</HEAD>
11<body MS_POSITIONING="GridLayout">
12<form id="Form1" method="post" runat="server">
13 <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None"
14 BorderWidth="1px" BackColor="White" CellPadding="4" ShowFooter="True" AutoGenerateColumns="False">
15 <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
16 <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
17 <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
18 <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
19 <Columns>
20 <asp:TemplateColumn HeaderText="Employee ID">
21 <ItemTemplate>
22 <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
23 </asp:Label>
24 </ItemTemplate>
25 <FooterTemplate>
26 <asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
27 </FooterTemplate>
28 <EditItemTemplate>
29 <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
30 </asp:TextBox>
31 </EditItemTemplate>
32 </asp:TemplateColumn>
33 <asp:TemplateColumn HeaderText="Last Name">
34 <ItemTemplate>
35 <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>'>
36 </asp:Label>
37 </ItemTemplate>
38 <FooterTemplate>
39 <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
40 </FooterTemplate>
41 <EditItemTemplate>
42 <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
43 </EditItemTemplate>
44 </asp:TemplateColumn>
45 <asp:TemplateColumn HeaderText="First Name">
46 <ItemTemplate>
47 <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstname") %>'>
48 </asp:Label>
49 </ItemTemplate>
50 <FooterTemplate>
51 <asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
52 </FooterTemplate>
53 <EditItemTemplate>
54 <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
55 </EditItemTemplate>
56 </asp:TemplateColumn>
57 </Columns>
58 <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
59 </asp:DataGrid>
60</form>
61</body>
62</HTML>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3<HTML>
4<HEAD>
5<title>WebForm1</title>
6<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
7<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
8<meta name="vs_defaultClientScript" content="JavaScript">
9<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10</HEAD>
11<body MS_POSITIONING="GridLayout">
12<form id="Form1" method="post" runat="server">
13 <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None"
14 BorderWidth="1px" BackColor="White" CellPadding="4" ShowFooter="True" AutoGenerateColumns="False">
15 <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
16 <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
17 <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
18 <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
19 <Columns>
20 <asp:TemplateColumn HeaderText="Employee ID">
21 <ItemTemplate>
22 <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
23 </asp:Label>
24 </ItemTemplate>
25 <FooterTemplate>
26 <asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
27 </FooterTemplate>
28 <EditItemTemplate>
29 <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
30 </asp:TextBox>
31 </EditItemTemplate>
32 </asp:TemplateColumn>
33 <asp:TemplateColumn HeaderText="Last Name">
34 <ItemTemplate>
35 <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>'>
36 </asp:Label>
37 </ItemTemplate>
38 <FooterTemplate>
39 <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
40 </FooterTemplate>
41 <EditItemTemplate>
42 <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
43 </EditItemTemplate>
44 </asp:TemplateColumn>
45 <asp:TemplateColumn HeaderText="First Name">
46 <ItemTemplate>
47 <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.firstname") %>'>
48 </asp:Label>
49 </ItemTemplate>
50 <FooterTemplate>
51 <asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
52 </FooterTemplate>
53 <EditItemTemplate>
54 <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
55 </EditItemTemplate>
56 </asp:TemplateColumn>
57 </Columns>
58 <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
59 </asp:DataGrid>
60</form>
61</body>
62</HTML>
InsertableDataGrid.aspx.vb:
1Imports System.Data
2Imports System.Data.SqlClient
3Public Class InserTableDataGrid
4 Inherits System.Web.UI.Page
5 Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
6Web Form Designer Generated Code
16 Dim connstr As String = "Integrated Security=SSPI;User ID=sa;Initial Catalog=NorthWind;Data Source=.\netsdk"
17 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
18 If Not Page.IsPostBack Then
19 BindGrid()
20 End If
21 End Sub
22 Sub BindGrid()
23 Dim cnn As New SqlConnection(connstr)
24 Dim da As New SqlDataAdapter("select employeeid,lastname,firstname from employees", cnn)
25 Dim ds As New DataSet()
26 da.Fill(ds, "employees")
27 DataGrid1.DataSource = ds
28 DataGrid1.DataBind()
29 End Sub
30 Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)_
31 Handles DataGrid1.ItemCommand
32 If e.CommandName = "Insert" Then
33 Dim cnn As New SqlConnection(connstr)
34 Dim t1 As TextBox = e.Item.FindControl("textbox2")
35 Dim t2 As TextBox = e.Item.FindControl("textbox4")
36 cnn.Open()
37 Dim cmd As New SqlCommand("insert into employees(lastname,firstname) values('" & t1.Text & "','" & t2.Text & "')", cnn)
38 cmd.ExecuteNonQuery()
39 cnn.Close()
40 BindGrid()
41 End If
42 End Sub
43End Class
2Imports System.Data.SqlClient
3Public Class InserTableDataGrid
4 Inherits System.Web.UI.Page
5 Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
6Web Form Designer Generated Code
16 Dim connstr As String = "Integrated Security=SSPI;User ID=sa;Initial Catalog=NorthWind;Data Source=.\netsdk"
17 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
18 If Not Page.IsPostBack Then
19 BindGrid()
20 End If
21 End Sub
22 Sub BindGrid()
23 Dim cnn As New SqlConnection(connstr)
24 Dim da As New SqlDataAdapter("select employeeid,lastname,firstname from employees", cnn)
25 Dim ds As New DataSet()
26 da.Fill(ds, "employees")
27 DataGrid1.DataSource = ds
28 DataGrid1.DataBind()
29 End Sub
30 Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)_
31 Handles DataGrid1.ItemCommand
32 If e.CommandName = "Insert" Then
33 Dim cnn As New SqlConnection(connstr)
34 Dim t1 As TextBox = e.Item.FindControl("textbox2")
35 Dim t2 As TextBox = e.Item.FindControl("textbox4")
36 cnn.Open()
37 Dim cmd As New SqlCommand("insert into employees(lastname,firstname) values('" & t1.Text & "','" & t2.Text & "')", cnn)
38 cmd.ExecuteNonQuery()
39 cnn.Close()
40 BindGrid()
41 End If
42 End Sub
43End Class