国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET在DataGrid快速添加新行

2024-07-10 13:12:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
   asp.net datagrid為我們提供的內(nèi)建的記錄行編輯功能,但是沒(méi)有提供內(nèi)建的添加新行的功能。一個(gè)辦法就是:在datatable中添加新行,然后再重新綁定到datagrid,這個(gè)辦法可行,但在更新前需要進(jìn)行確認(rèn),可能會(huì)產(chǎn)生空行。另外一個(gè)解決辦法就是:利用datagrid footer template來(lái)提供一個(gè)空的行,這樣既可以提高速度,也可以避免其它方法帶來(lái)的不足。
  
  為了為瀏覽者提供一個(gè)空行,我們使用datagrid的footer template,我們直接在footer template里添加文本框,這樣可以避免不必要的操作:比如點(diǎn)擊“編輯”按鈕等。這樣也可以減少往復(fù)數(shù)據(jù)提交的次數(shù)。我們這里仍然linkbutton(插入),并設(shè)置commandname屬性為“insert”,這個(gè)commandname在datagrid的itemcommand事件中,確保只有用戶(hù)點(diǎn)擊了“insert”linkbutton才添加記錄。添加到數(shù)據(jù)庫(kù)的方法是很簡(jiǎn)單的。
  
  下面的這個(gè)例子提供了datagrid快速添加新行的功能。aspx代碼和cohe behind代碼分別如下,注意更改數(shù)據(jù)錄連接字符串:
  
  查看例子
  
  insertabledatagrid.aspx
  
  <%@ page language="<a target="_blank">vb</a>" autoeventwireup="false" codebehind="insertabledatagrid.aspx.vb" inherits="aspx<a 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 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
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵台县| 磐石市| 广宁县| 东阿县| 阿克陶县| 巴林左旗| 富民县| 蛟河市| 呼伦贝尔市| 图们市| 自治县| 双流县| 舞钢市| 富川| 东方市| 文水县| 息烽县| 舟曲县| 余江县| 阳山县| 东城区| 呼伦贝尔市| 炉霍县| 博湖县| 宝清县| 当涂县| 左权县| 鄂伦春自治旗| 新郑市| 界首市| 陵川县| 花垣县| 漳平市| 巴彦县| 湖南省| 贵德县| 轮台县| 塘沽区| 平利县| 邹城市| 黄石市|