Asp. NET quickly adds new rows to the DataGrid

Source: Internet
Author: User
Tags eval insert visual studio
The Asp.net|datagrid asp.net datagrid provides us with the built-in record line editing functionality, but does not provide built-in functionality to add new rows. One way to do this is to add a new row to the DataTable, and then rebind it to the DataGrid, which works, but it needs to be validated before it is updated, and a blank line may be generated. Another solution is to use the DataGrid footer template to provide an empty line, which can either increase speed or avoid the drawbacks of other methods.

To provide a blank line for the viewer, we use the footer Template of the DataGrid, and we add a text box directly to footer Template to avoid unnecessary actions such as clicking on the Edit button. This can also reduce the number of reciprocating data submissions. We still LinkButton (insert) and set the CommandName property to "Insert", this commandname in the ItemCommand event of the DataGrid to ensure that only the user clicked "Insert" LinkButton only add records. The method to add to the database is simple.

The following example provides the ability for the DataGrid to quickly add new rows. The ASPX code and the Cohe behind code are as follows, noting that changing the data recording connection string:

View examples

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&gt"; 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=" >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>
<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= "A-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 are 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-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.