• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

How do I create a loop in vb.net?

Hi experts,

The sub routine below is suposed to loop through and write to four hyperlink labels, at the moment it is only writing to one hyperllink...

Any help on the code to write to four hyperlink labels would be appreciated.
Private Sub ShowLinks(ByVal nMonth As Integer)
        Try
            aPathNodeIt = aPathNav.Select("months/month_" & nMonth.ToString("00") & "/link")
 
            For Each aPathNode In aPathNodeIt
 
                aPathNodeIt2 = aPathNode.Select("url")
                aPathNodeIt2.MoveNext()
                HyperLink1.NavigateUrl = aPathNodeIt2.Current.Value
 
                aPathNodeIt2 = aPathNode.Select("text")
                aPathNodeIt2.MoveNext()
                HyperLink1.Text = aPathNodeIt2.Current.Value
            Next
        Catch ex As Exception
            Page.Response.Write("No link define")
        End Try
    End Sub

Open in new window

0
Zoooink
Asked:
Zoooink
  • 8
  • 4
  • 4
3 Solutions
 
srikanthreddyn143Commented:
What is apathnodeit and apathnode
0
 
JRockFLCommented:
You probablly only have one record in aPathNodeIt and that is why it is only displaying once.

Put a break point at  For Each aPathNode In aPathNodeIt and then step through the code.
0
 
ZoooinkAuthor Commented:
apathnode, apathnodeit and apathnodeit2 are variables which obtain data from an XML file.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
srikanthreddyn143Commented:
I agree with JRockFL. Go ahead with that.
0
 
ZoooinkAuthor Commented:
Hi guys, I've got four records in the XML file, I think what is happening is that each time the code is looping it is writing to the first link only and overwriting it four times, which is probably why I'm getting one link show up, but how do I modify the code so that it writes to the four separate link controls?
0
 
srikanthreddyn143Commented:
Keep an inner for loop to loop through the 4 hyperlink controls
0
 
ZoooinkAuthor Commented:
srikanthreddyn143,

Could I ask you to insert the code as I'm not sure where to place it, there are four link controls, HyperLink1, HyperLink2, HyperLink3, HyperLink4

thanks.
0
 
JRockFLCommented:
You can try something like this...
I used a hashtable as my datastore for this example
Or another option would be to use a asp:literal control instead of the hyperlinks.
        Dim hsh As New Hashtable()
        hsh.Add("Link1", "Link1.aspx")
        hsh.Add("Link2", "Link2.aspx")
        hsh.Add("Link3", "Link3.aspx")
 
        For Each item As DictionaryEntry In hsh
            Dim link As New HyperLink
            link.ID = "hl" & item.Key
            link.Text = item.Key
            link.NavigateUrl = item.Value
            Me.Controls.Add(link)
        Next

Open in new window

0
 
ZoooinkAuthor Commented:
JRockFL,

Thanks for the input, but I would like to stick to the code I have produced and just modify it as the trouble is how my existing aspx page is currently formatted, the links sit within a table of four rows, and if I use a literal, then I will have to redesign the layout of the page completely which is something I am trying to avoid.
0
 
JRockFLCommented:
Zoooink,
The code I provided was an example. You can modify it fit your needs.
0
 
ZoooinkAuthor Commented:
Thanks JRockFL,

I'm having trouble inserting it in to my code though, could you show me where to insert it?
0
 
srikanthreddyn143Commented:
How do you know to which specific hyperlink control the specific url should be added?
0
 
JRockFLCommented:
Something like this. You might need to add a placeholder though to handle the location of the links.
            For Each aPathNode In aPathNodeIt
 	       Dim HyperLink1 As New HyperLink
                aPathNodeIt2 = aPathNode.Select("url")
                aPathNodeIt2.MoveNext()
                HyperLink1.NavigateUrl = aPathNodeIt2.Current.Value
 
                aPathNodeIt2 = aPathNode.Select("text")
                aPathNodeIt2.MoveNext()
                HyperLink1.Text = aPathNodeIt2.Current.Value		
	       Me.Controls.Add(HyperLink1)
            Next

Open in new window

0
 
ZoooinkAuthor Commented:
srikantreddyn143,

It doesn't matter which hyperlink control any given url goes in to, the XML file handles the order of each url, as in they are already listed in order.

JRockFL,

Thank you for the code, but that has put me back to square one, lol, my oroginal code could list the urls on the fly, but the problem I had was that I could not apply formats, layouts etc, without having to write extra code, or even re-write the entire page in code.

Perhaps a counter might be something I need? since the four hyperlink controls already exist on the page, I think all I need is some kind of inner loop which counts 1 to 4 and at each step applies the url to the hyperlink control? What do you think guys?
0
 
ZoooinkAuthor Commented:
This is really driving me insane, I've now managed to populate two of the four link controls (HyperLink1 and HyperLink2) but the code is only picking up the last record from the XML file and populating those two hyperlink controls, surely this isn't as hard as it seems guys?? lol, I've supplied my modified code below. At the end of the code is a smaple XML file.
Imports System.Xml
 
Partial Class template
    Inherits System.Web.UI.Page
 
    Private aPathNav As System.Xml.XPath.XPathNavigator
    Private aPathDoc As System.Xml.XPath.XPathDocument
    Private aPathNodeIt As System.Xml.XPath.XPathNodeIterator
    Private aPathNode As System.Xml.XPath.XPathNavigator
    Private aPathNodeIt2 As System.Xml.XPath.XPathNodeIterator
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        aPathDoc = New System.Xml.XPath.XPathDocument("H:\websites_2008\vqwe\LinksFile.xml")
        aPathNav = aPathDoc.CreateNavigator()
        ShowLinks(Now.Month)
    End Sub
 
    Private Sub ShowLinks(ByVal nMonth As Integer)
 
        Try
            aPathNodeIt = aPathNav.Select("months/month_" & nMonth.ToString("00") & "/link")
 
            For Each aPathNode In aPathNodeIt
                aPathNodeIt2 = aPathNode.Select("url")
                aPathNodeIt2.MoveNext()
                HyperLink1.NavigateUrl = aPathNodeIt2.Current.Value
                aPathNodeIt2 = aPathNode.Select("text")
                aPathNodeIt2.MoveNext()
                HyperLink1.Text = aPathNodeIt2.Current.Value
 
                If HyperLink1.Text <> "" Then
                    aPathNodeIt2 = aPathNode.Select("url")
                    aPathNodeIt2.MoveNext()
                    HyperLink2.NavigateUrl = aPathNodeIt2.Current.Value
                    aPathNodeIt2 = aPathNode.Select("text")
                    aPathNodeIt2.MoveNext()
                    HyperLink2.Text = aPathNodeIt2.Current.Value
                ElseIf HyperLink2.Text <> "" Then
                    aPathNodeIt2 = aPathNode.Select("url")
                    aPathNodeIt2.MoveNext()
                    HyperLink3.NavigateUrl = aPathNodeIt2.Current.Value
                    aPathNodeIt2 = aPathNode.Select("text")
                    aPathNodeIt2.MoveNext()
                    HyperLink3.Text = aPathNodeIt2.Current.Value
                ElseIf HyperLink3.Text <> "" Then
                    aPathNodeIt2 = aPathNode.Select("url")
                    aPathNodeIt2.MoveNext()
                    HyperLink4.NavigateUrl = aPathNodeIt2.Current.Value
                    aPathNodeIt2 = aPathNode.Select("text")
                    aPathNodeIt2.MoveNext()
                    HyperLink4.Text = aPathNodeIt2.Current.Value
                End If
            Next
 
        Catch ex As Exception
            Page.Response.Write("No link define")
        End Try
 
    End Sub
 
End Class
 
 
-----------------------
 
 
XML File
 
<?xml version="1.0" encoding="utf-8" ?>
<months>
  <month_04>
    <link><url>http://www.a.com</url><text>link a</text></link>
    <link><url>http://www.b.com</url><text>link b</text></link>
    <link><url>http://www.c.com</url><text>link c</text></link>
    <link><url>http://www.d.com</url><text>link d</text></link>
  </month_04>
</months>

Open in new window

0
 
ZoooinkAuthor Commented:
Ok, so I've gone a completely different way to solving this issue, instead of using code to interrogate the XML file, I've used a repeater in the page to display the links, and just referenced the XML file in the code behind so that it still changes the links on a month by month basis.

Thanks for the time and effort guys
<!--- here's the page code --->
 
<div>
<asp:Repeater ID="repeater1" runat="server" DataSourceID="xmlDS">
<HeaderTemplate>
<table border="0" style="vertical-align: middle; width: 50%; color: white; font-family: Arial; height: 21px; text-align: left;" cellpadding="2" cellspacing="2">
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: gray;">
<td>
<asp:HyperLink ID="lnkUrl" runat="server" Text='<%# XPath("text") %>' NavigateUrl='<%# XPath("url") %>' ForeColor="White" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:XmlDataSource runat="server" ID="xmlDS" DataFile="~/LinksFile.xml"></asp:XmlDataSource>
</div>
 
--------------------------------
 
'here's the code behind, which in the end, wasn't very much at all
 
Imports System.Xml
 
Partial Class template
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ShowLinks(Now.Month)
    End Sub
 
    Private Sub ShowLinks(ByVal nMonth As Integer)
        xmlDS.XPath = ("months/month_" & nMonth.ToString("00") & "/link")
    End Sub
End Class

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now