Use the gridview to display the primary table and add the function of enabling and disabling

Source: Internet
Author: User

In this example, the nested gridview is used to display the primary table, and JavaScript is used to control the display and hiding of the detail table. It is worth noting that in the rowdatabound event of the gridview, do not open the Database Multiple times. Otherwise, the number of connections will soon become full.

For the database in the example, refer to the database attached to the CD in ASP. NET 2.0 application development technology.

View examples

Code:

& Lt; % @ page Language = "C #" autoeventwireup = "true" codefile = "gridviewnested. aspx. CS "inherits =" exam_gridviewnested "% & gt; <br/> & lt ;! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" & gt; <br/> & lt; html xmlns = "http://www.w3.org/1999/xhtml" & gt; <br/> & lt; head runat = "server" & gt; <br/> & lt; Title & gt; use the gridview to display the primary table and add the function of enabling and disabling & lt;/Title & gt; <br/> & lt; style type = "text/CSS" & gt; <br/> TD, Div, A {font-size: 12px} <br/> & lt;/Style & gt; <br/> & lt; /Head & gt; <br/> & lt; Body & gt; <br/> & lt; Form ID = "form1" runat = "server" & gt; <br/> & lt; ASP: gridview id = "mastergridview" runat = "server" autogeneratecolumns = "false" width = "760px" <br/> borderwidth = "1" onrowdatabound = "mastergridview_rowdatabound" datakeynames = "ID" <br/> showheader = "false" & gt; <br/> & lt; columns & gt; <br/> & lt; ASP: templatefield & gt; <br/> & lt; itemtemplate & gt; <br/> & lt; Div style = "width: 100%; padding: 2px; font-weight: bold; Background-color: # dedede; <br/> float: left "& gt; <br/> & lt; span style =" float: Left "& gt; Topic name: & lt ;%# eval (" title ") % & gt; & lt;/span & gt; & lt; span style = "float: Right; <br/> color: red; cursor: pointer "onclick =" showhidden ('& lt; % # eval ("ID") % & gt;', event) "& gt; hide & lt;/span & gt; & lt;/Div & gt; <br/> & lt; Div style = "background-color: # FFF; padding-left: 60px; clear: both "id =" Div & lt; % # eval ("ID") % & gt; "& gt; <br/> & lt; ASP: gridview id = "detailgridview" runat = "server" autogeneratecolumns = "false" showheader = "true" <br/> width = "100%" horizontalalign = "Left" & gt; <br/> & lt; headerstyle backcolor = "# 9999ff"/& gt; <br/> & lt; columns & gt; <br/> & lt; ASP: templatefield headertext = "<SPAN class = 'wp _ keywordlink '> Article </span> name" & gt; <br/> & lt; itemtemplate & gt; <br/> & lt; a href = "/article/& lt; % # eval (" objectguid ") % & gt;/read. aspx "& gt; <br/> & lt; % # eval (" title ") % & gt; <br/> & lt;/A & gt; [& lt; % # eval ("hitcount") % & gt;] <br/> & lt;/itemtemplate & gt; <br/> & lt;/ASP: templatefield & gt; <br/> & lt; ASP: boundfield headertext = "Release Date" datafield = "createdate" htmlencode = "false" dataformatstring = "{0: yyyy-mm-dd} "<br/> itemstyle-width =" 100px "itemstyle-horizontalalign =" center "/& gt; <br/> & lt; /columns & gt; <br/> & lt;/ASP: gridview & gt; <br/> & lt;/Div & gt; <br/> & lt; /itemtemplate & gt; <br/> & lt;/ASP: templatefield & gt; <br/> & lt;/columns & gt; <br/> & lt; /ASP: gridview & gt; <br/> & lt;/Form & gt; <br/> & lt;/Body & gt; <br/> & lt; /html & gt; <br/>

CS:

Using system; <br/> using system. data; <br/> using system. data. oledb; <br/> using system. configuration; <br/> using system. collections; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. htmlcontrols; <br/> Public partial class exam_gridviewnested: system. web. UI. page <br/>{< br/> string connectionstring = @ "prov Ider = Microsoft. jet. oledb.4.0; Data Source = | datadirectory | \ aspxweb. MDB; persist Security info = true "; <br/> oledbconnection Cn1; <br/> protected void page_load (Object sender, eventargs E) <br/>{< br/> If (! Page. ispostback) <br/>{< br/> oledbconnection Cn = new oledbconnection (connectionstring); <br/> CN. open (); <br/> Cn1 = new oledbconnection (connectionstring); <br/> cn1.open (); <br/> oledbcommand cmd = new oledbcommand ("select * from [subject]", CN); <br/> oledbdatareader DR = cmd. executereader (commandbehavior. closeconnection); <br/> mastergridview. datasource = Dr; <br/> mastergridview. databind (); <br/> dr. c Lose (); <br/> cmd. dispose (); <br/> CN. dispose (); <br/> cn1.dispose (); <br/> Cn = Cn1 = NULL; <br/>}< br/> protected void mastergridview_rowdatabound (Object sender, gridviewroweventargs e) <br/>{< br/> If (E. row. rowtype = datacontrolrowtype. datarow) <br/>{< br/> gridview ogridview = (gridview) E. row. findcontrol ("detailgridview"); <br/> If (ogridview! = NULL) <br/>{< br/> oledbcommand cmd = new oledbcommand ("select top 10 * from document where pid =" + mastergridview. datakeys [E. row. rowindex]. value, Cn1); <br/> oledbdatareader DR1 = cmd. executereader (); <br/> ogridview. datasource = DR1; <br/> ogridview. databind (); <br/> dr1.close (); <br/> cmd. dispose (); <br/>}< br/>

VB. NET:

Private connectionstring as string = "provider = Microsoft. jet. oledb.4.0; Data Source = | datadirectory | \ aspxweb. MDB; persist Security info = true "<br/> private Cn1 as oledbconnection <br/> protected sub page_load (byval sender as object, byval e as eventargs) <br/> if not page. ispostback then <br/> dim cn as oledbconnection = new oledbconnection (connectionstring) <br/> CN. open <br/> Cn1 = new oledbconnection (connectionstring) <br/> cn1.open <br/> dim cmd as oledbcommand = new oledbcommand ("select * from [subject]", CN) <br/> dim Dr as oledbdatareader = cmd. executereader (commandbehavior. closeconnection) <br/> mastergridview. datasource = Dr <br/> mastergridview. databind <br/> dr. close <br/> cmd. dispose <br/> CN. dispose <br/> cn1.dispose <br/> Cn = Cn1 = nothing <br/> end if <br/> end sub <br/> protected sub mastergridview_rowdatabound (byval sender as object, byval e as gridviewroweventargs) <br/> If e. row. rowtype = datacontrolrowtype. datarow then <br/> dim ogridview as gridview = ctype (E. row. findcontrol ("detailgridview"), gridview) <br/> if not (ogridview is nothing) then <br/> dim cmd as oledbcommand = new oledbcommand ("select top 10 * from document where pid =" + mastergridview. datakeys (E. row. rowindex ). value, Cn1) <br/> dim DR1 as oledbdatareader = cmd. executereader <br/> ogridview. datasource = DR1 <br/> ogridview. databind <br/> dr1.close <br/> cmd. dispose <br/> end if <br/> end sub <br/>

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.