ASP.NET在DataGrid快速添加新行
2024-07-10 13:12:07
供稿:网友
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