HOW TO: Use nested Repeater controls and Visual Basic .NET display hierarchical data

xiaoxiao2021-03-06  70

How to: display hierarchical data by using nested review

Article ID: 326338last Review: July 30, 2003Revision: 4.0

This Article Was Previously Published Under Q326338

For a Microsoft Visual C # .NET VERSION OF This Article, See

306154.

This Article Refers To The Following Microsoft .NET Framework Class Library Namespaces:

• System.Data • System.Data.sqlclient

In this task

Summary

• Bind to the parent Table • Bind to the child Table • Complete code list • References

On this Page

Summaryreferences

Summary

This Article Describes How To Use Nested

Repeater Web Controls to Display Hierarchical Data. You CAN Apply this Controls.

Back to the top

Bind to the Parent Table

1.Start Microsoft Visual Studio .Net. 2.on The File Menu, Point To New, and The Click Project. 3.Click Visual Basic Projects Under Project Types, and the Click ASP.NET Web Application Under Templates. 4.in The Location Box, Delete WebApplication #, and THEN TYPE

NestedRepeater

. If you use the local server, you can leave the server name as http:. // localhost The path appears as follows in the Location box: http: // localhost / NestedRepeater5.In Solution Explorer, right-click the project-name node (NestedRepeater), click Add, and then click Add Web Form.6.In the Name box, type NestedRepeater, and click Open.7.The new Web Form is created. It opens in Design View in the IDE of Microsoft Visual Studio .NET. From the toolbox, select the Repeater control, and then drag it to the Web Form page.8.Change the ID property of this Repeater control to parentRepeater.9.Switch to the HTML view for this Web Form. to do so Click The HTML Tab in The Lower-Left Corner of The Designer. The Html That The Repeater Control Genereates IS FOLLOWS: 10.Add The Following Code in The Repeater Tags:

<% # databinder.eval (container.dataitem, "au_id")%>

After you add the code, the html code for the reteater is ask:

<% # databinder.eval (container.dataitem, "au_id")%>

11.In Solution Explorer, right-click the NestedRepeater.aspx, and then click View Code to switch to the code-behind page (NestedRepeater.aspx.vb) .12.Add the following namespace declaration to the top of the file: Imports System.data

Imports system.data.sqlclient

13.Add the following code to the Page_Load event to create a connection to the Pubs database, and to bind the Authors table to the Repeater control: Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Loaddim cnn as sqlconnection = new sqlConnection ("Server = (local); database = pubs; integrated security = sspi")

DIM cmd1 as sqldataadapter = new SqldataAdapter ("Select * from authors", CNN)

DIM DS AS DATASET = New Dataset ()

cmd1.fill (DS, "Authors")

'INSERT Code in Step 4 of the next section here.

ParentrePeater.DataSource = DS.TABLES ("authors")

Page.DATABIND ()

CNN.Close ()

End Sub

NOTE: You may have to modify the database connection string as appropriate for your environment.14.Save all of the files.15.On the Build menu, click Build Solution to compile the project.16.View the .aspx page in the browser And the Verify That The page Works so far. The Output Should Appear as Follows:

172-32-1176

213-46-8915

238-95-7766

267-41-2394

...

Back to the top

Bind to the child table

1.in The HTML View of the NestedRepeater.aspx Page, Locate The Following Code: <% # DataBinder.eval (Container.DataItem, "Au_ID")%>
.

After this line of code to add a second reperter control to the itemtemplate of the parent review = "childreteater" runat = "limited"> Runat = "server">

<% # Container.dataitem ("Title_ID")%>


2.Set the DataSource property for the child Repeater control as follows: After you set the DataSource property for The Child Repeater Control, The Html Code for the Two Repeater Controls (PARENT AND CHILD) APPEARS AS FOLLOWS:

<% # Databinder.eval (container.dataitem, "au_id")%>


DataSource = '<% #

Container.DataItem.row.getChildrows ("MyRelation")%>>>

<% # Container.DataItem ("Title_ID")%>

3.Add the following page: <% @ Import namespace = "system.data"%>

4.IN The Code-Behind page, Replace The next 'Insert Code in Step 4 of the next section.

with the following code, which adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables: Dim cmd2 As SqlDataAdapter = New SqlDataAdapter ( "select * from titleauthor", cnn)

Cmd2.fill (DS, "Titles")

DS.RELATIONS.ADD ("MyRelation", _

DS.TABLES ("Authors"). Column ("au_id"), _

DS.Tables ("Titles"). Column ("au_id"))

ParentrePeater.DataSource = DS.TABLES ("authors")

5.Save all files, and then build the project.6.view the page in the page Works so far. The output shouth as stock: 172-32-1176

PS3333

213-46-8915

BU1032

Bu2075

238-95-7766

PC1035

267-41-2394

BU1111

TC7777

...

Back to the top

Complete code list

NestedRepeater.aspx

<% @ Import namespace = "system.data"%>

<% @ Page language = "vb" autoeventwireup = "false" codebehind = "nestedrepeater.aspx.vb" inherits = "YourProjectNamespace.NESTEDREPEATER"%>

NestedRepeater </ Title></p> <p><meta name = "generator" content = "Microsoft Visual Studio.Net 7.0"></p> <p><meta name = "code_language" content = "Visual Basic 7.0"></p> <p><meta name = "vs_defaultclientscript" content = "javascript"></p> <p><meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5"></p> <p></ HEAD></p> <p><body ms_positioning = "gridLayout"></p> <p><form id = "form1" method = "post" runat = "server"></p> <p><ask: repeater ID = "ParentrePeater" runat = "server"></p> <p><itemtemplate></p> <p><b></p> <p><% # Databinder.eval (container.dataitem, "au_id")%></p> <p></ b></p> <p><br></p> <p><ask: repeater id = "childrepeater"</p> <p>Runat = "Server" DataSource = '<% # Container.DataItem.row.getChildrows ("MyRelation")%>>>>></p> <p><itemtemplate></p> <p><% # Container.DataItem ("Title_ID")%> <br></p> <p></ itemtemplate> </ ask: review> </ ask:</p> <p></ itemtemplate></p> <p></ ask: repeater></p> <p></ form></p> <p></ body></p> <p></ Html></p> <p>NestedRepeater.aspx.vb</p> <p>Imports system.data</p> <p>Imports system.data.sqlclient</p> <p>Public Class NestedRepeater</p> <p>Inherits System.Web.ui.page</p> <p>Protected Withevents Childrepeater As System.Web.ui.WebControls.Repeater</p> <p>Protected Withevents Parentrepeater As System.Web.ui.WebControls.repeater</p> <p>#Region "Web Form Designer Generated Code"</p> <p>'This Call is Required by the Web Form Designer.</p> <p><System.diagnostics.debuggerstepthrough ()> private subinitializecomponent ()</p> <p>End Sub</p> <p>Private sub page_init (byval sender as system.object, byval e as system.eventargs) Handles mybase.init</p> <p>'Codegen: This Method Call is Required by the Web Form Designer</p> <p>'Do Not Modify It Using The Code Editor.</p> <p>InitializeComponent ()</p> <p>End Sub</p> <p>#End region</p> <p>Private Sub Page_Load (Byvale AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD</p> <p>DIM CNN As SqlConnection = New SqlConnection ("Server = (local); database = pubs; integrated security = sspi")</p> <p>DIM cmd1 as sqldataadapter = new SqldataAdapter ("Select * from authors", CNN)</p> <p>DIM DS AS DATASET = New Dataset ()</p> <p>cmd1.fill (DS, "Authors")</p> <p>DIM CMD2 As SqldataAdapter = New SqldataAdapter ("Select * from titleauthor", cnn)</p> <p>Cmd2.fill (DS, "Titles")</p> <p>DS.RELATIONS.ADD ("MyRelation", _</p> <p>DS.TABLES ("Authors"). Column ("au_id"), _</p> <p>DS.Tables ("Titles"). Column ("au_id"))</p> <p>ParentrePeater.DataSource = DS.TABLES ("authors")</p> <p>ParentrePeater.DataSource = DS.TABLES ("authors")</p> <p>Page.DATABIND ()</p> <p>CNN.Close ()</p> <p>End Sub</p> <p>END CLASS</p> <p>Back to the top</p> <p>References</p> <p>For more information, Visit The Following Topics in The Microsoft .NET Framework Software Development Kit (SDK):</p> <p>Add a rellationship Between Tables</p> <p>Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConaddingReranceShipBetwentWotables.asp</p> <p>NaviGating a Relationship Between Tableshi</p> <p>Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConnavigatingReranceShipBetWeentWotables.asp</p> <p>REPEATER Web Server Control</p> <p>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebserverControl.asp</p> <p>Back to the top</p> <p>Applies TO</p> <p>• Microsoft ASP.NET (included with the .NET Framework 1.1) • Microsoft ASP.NET (included with the .NET Framework) 1.0 • Microsoft Visual Basic .NET 2003 Standard Edition • Microsoft Visual Basic .NET 2002 Standard Edition</p> <p>Top of page</p> <p>Keywords: kbdatabinding kbhowtomaster kbserverControls KB326338</p> <p>Top of page</p> <p><script type = text / javascript> var g_asanswers = []; var cr_ianswerptr = 0; var currentsectionID = 1; var navindex = 1; var netsection; var navpath = new array (); var g_scid = 'sw; eN; 1075' ; var g_SITE = 'gn'; var g_REGIONID = 'zh-cn'; var g_URL = '% 252fdefault.aspx% 252fkb% 252f326338% 252fEN-US% 252f'; var g_BROWSERLANGCODE = 'zh-cn'; var g_SURVEYLANGCODE = ' En '; var g_surveyid =' 1075 '; var g_surveyname ='; var g_asparams = ['kb', 'en-us', '326338', '', '', ',' ',', ',' ',' ']; var g_showall = 0; var g_surveystyle =' popup '; var bclickcancel = 0; var g_maxkbsincel = 0; var g_maxkbsincookie =' 10 '; var g_kbvisited ='; </ script> Personal Information Center |</p> <p>contact us</p> <p>© 2004 Microsoft Corporation. All rights reserved.</p> <p>Keep all rights |</p> <p>Trademark |</p> <p>Privacy statement</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-91715.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="91715" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.051</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '1XZbdkYthCo13B53Pdmi03DqVfF1zQTE_2FRqDsxRycVyiol5bqwwG_2BZ6YQIUTaL89EkFr64E5lg_2FjTFWX7TW36A_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>