Hi,

I have an application as in the following,

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = String.Empty

        If Page.IsPostBack Then
            mess.Text = "<p style='margin-left:-2em'>You selected <b>" & drop1.SelectedItem.Text & "</b></p>"
        End If
        Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)
        
        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
       
        Dim song As XmlNode
        For Each song In songList
    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<p class='music'><b>Title:</b> " & title.InnerText & "<br/>"

            Dim category As XmlNode = title.NextSibling
            songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
    
            Dim album As XmlNode = category.NextSibling
            Dim artist As XmlNode = album.NextSibling
            songNodesOut.Text &= "<b>Artist:</b>" & artist.InnerText & "<br/>"
            songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
   
        Next		
    End Sub
</script>
<html>
<head/>
            <body>      
             <div id="main">
	    <form id="Form1" runat="server">
            <asp:DropDownList id="drop1" runat="server">
            <asp:ListItem>Blues</asp:ListItem>
            <asp:ListItem>Light Rock</asp:ListItem>
            </asp:DropDownList>
           
           <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server"/>
           <asp:Label id="songNodesOut" runat="server" />
         </form>
                </div>
            </body>
        </html>

The application works, but I would like to have 10 results per page. How easily can this be done? I don't want to use data view or grid view like most of the examples talk about, I would like to do this in the fashion that looks like what I have now, only not so many entries in a whole page.

Thanks for your help.

kvprajapati commented: Good question. +10

Recommended Answers

All 22 Replies

This is not that difficult of a task. The first thing I would do on the page is calculate the number of "pages" you have with your data:

Dim RecordsPerPage As Integer = 10 
Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1

You need to keep track of which "page" you are displaying which you could pass through the query string:

Dim CurrentPage As Integer = 1

If Request.QueryString("Page") IsNot Nothing AndAlso Request.QueryString("Page").Trim <> String.Empty Then
    CurrentPage = CInt(Request.QueryString("Page"))
End If

Now you know haw many "pages" of data you have and what page you should be displaying (defaults to one if not passed to the page). Now you need to change your For loop to only loop through the items you need to display.

First I would calculate which items we need to display:

Dim StartRecord As Integer = (CurrentPage - 1) * RecordsPerPage
Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)

Your beginning of your For loop should then look something like this:

Dim song As XmlNode
For i As Integer = StartRecord To EndRecord
    song = songList.Item(i)
........

The rest of the code can stay as it is. Now for navigation you can go as fancy as you would like. You can programatically add links to individual pages of data or previuous and next links. Any link would just need to point to itself passing a page number: <a href="mypage.aspx?Page=2">Page 2</a>

Let me know if this doesn't make sense. I didn't actually test the code; i just typed it on here. I also assumed with this sample that the XmlNodeList object is zero based.. I've never used it before. Let me know how it goes.

Thanks,
Kenny

This is not that difficult of a task. The first thing I would do on the page is calculate the number of "pages" you have with your data:

Dim RecordsPerPage As Integer = 10 
Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1

You need to keep track of which "page" you are displaying which you could pass through the query string:

Dim CurrentPage As Integer = 1

If Request.QueryString("Page") IsNot Nothing AndAlso Request.QueryString("Page").Trim <> String.Empty Then
    CurrentPage = CInt(Request.QueryString("Page"))
End If

Now you know haw many "pages" of data you have and what page you should be displaying (defaults to one if not passed to the page). Now you need to change your For loop to only loop through the items you need to display.

First I would calculate which items we need to display:

Dim StartRecord As Integer = (CurrentPage - 1) * RecordsPerPage
Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)

Your beginning of your For loop should then look something like this:

Dim song As XmlNode
For i As Integer = StartRecord To EndRecord
    song = songList.Item(i)
........

The rest of the code can stay as it is. Now for navigation you can go as fancy as you would like. You can programatically add links to individual pages of data or previuous and next links. Any link would just need to point to itself passing a page number: <a href="mypage.aspx?Page=2">Page 2</a>

Let me know if this doesn't make sense. I didn't actually test the code; i just typed it on here. I also assumed with this sample that the XmlNodeList object is zero based.. I've never used it before. Let me know how it goes.

Thanks,
Kenny

I am sure I must have done something really wrong here, because I get this error telling me Compiler Error Message: BC30337: 'For' loop control variable cannot be of type 'System.Xml.XmlNode'.

Here is my code, after reading all your careful edits, but I am not sure where I am supposed to implement the part that request the Page variable to tell what page I am on. I am sure there are some really weird errors, so please feel free to let me know.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>
<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = "" 
        mess.Text = ""

        Dim file As String = Context.Server.MapPath("music.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")

        Dim RecordsPerPage As Integer = 10 
        Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
        If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
        
        Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If

        Dim StartRecord As Integer = (CurrentPage-1) * RecordsPerPage + 1
        Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
        If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)  

        If CurrentPage < TotalPageCount 
              
            songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
            mess.Text += "There are " & TotalPageCount & "pages.<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"
         
            For song As XmlNode 
            For i As Integer = StartRecord to EndRecord
            song = songList.Item(i)
            songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"

            Dim category As XmlNode = title.NextSibling
            songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
    
            Dim album As XmlNode = category.NextSibling
            Dim artist As XmlNode = album.NextSibling
            songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
            songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
   
            Dim date1 As XmlNode = artist.NextSibling
            Dim dateAttribute As XmlAttribute = date1.Attributes("added")
            songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>" 
         
         Next   
     Next 
       End If
     Else 
           <%--  Do nothing --%>
        End If

        mess.Text += "<p>You selected <b>" & drop1.SelectedItem.Text & " " & count & " items</b>. That is " & IntegerPart &  " pages</p>"
        mess.Text += "<br />Items for page " & page_no & " is: " & item_number_first & " to " & item_number_last & "<br />"                    
	
    End Sub
</script>
<html>
<head/>   
<body>
        <form id="Form1" runat="server">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my collection" runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
                <asp:ListItem>Rock</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server" />
        <asp:Label ID="songNodesOut" runat="server" />
        </form>
</body>
</html>

Thanks for your help.

Found a small typo in your code. Right before the main for loop remove this entire line:

For song As XmlNode

Let me know. I hope that works. I apologize as I didn't have time to create a whole test project.

Thanks,
Kenny

Actually, that should have just been changed to a Dim statement.

Change
For song As XmlNode

TO

Dim song As XmlNode

Kenny

Actually, that should have just been changed to a Dim statement.

Change
For song As XmlNode

TO

Dim song As XmlNode

Kenny

Thanks, and I have updated the code to the following:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>
<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = "" 
        mess.Text = ""

        Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")

        Dim RecordsPerPage As Integer = 10 
        Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
        If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
        
        Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If

        Dim StartRecord As Integer = (CurrentPage-1) * RecordsPerPage + 1
        Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
        If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)  
        If CurrentPage < TotalPageCount 
              
            songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
            mess.Text += "There are " & TotalPageCount & "pages.<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"
         
            Dim song As XmlNode 
            For i As Integer = StartRecord to EndRecord
            song = songList.Item(i)
            songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"

            Dim category As XmlNode = title.NextSibling
            songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
    
            Dim album As XmlNode = category.NextSibling
            Dim artist As XmlNode = album.NextSibling
            songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
            songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
   
            Dim date1 As XmlNode = artist.NextSibling
            Dim dateAttribute As XmlAttribute = date1.Attributes("added")
            songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>" 
        
     Next 
     Else 
           <%--  Do nothing --%>
        End If

        mess.Text += "<p>You selected <b>" & drop1.SelectedItem.Text & " " & songList.Count & " items</b>. That is " & TotalPageCount &  " pages</p>"
        mess.Text += "<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"                    
	
    End Sub
</script>
<html>
<head/>
<body>
    <div id="main">
        <form id="Form1" runat="server">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my collection"
                    runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
                <asp:ListItem>Rock</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server" />
        </div>
        <asp:Label ID="songNodesOut" runat="server" />
        </form>
        </div>
</body>
</html>

The code works from the main interface, but it looks like it does not print any results. Have I missed something here?

Thanks for your help.

Can you send me the xml file? kdion at kdion dot com
I would like to actually step through the code using your file.

Thanks,
Kenny

Hi,

Looks like I fixed the problem I was mentioning earlier by fixing the code.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>
<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = "" 
        mess.Text = ""

        Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")

        Dim RecordsPerPage As Integer = 10 
        Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
        If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
        
        Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If

        Dim StartRecord As Integer = (CurrentPage-1) * RecordsPerPage + 1
        Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
        If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)  

        If CurrentPage < TotalPageCount 
              
            songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")            
            mess.Text += "<p>You selected <b>" & drop1.SelectedItem.Text & " " & songList.Count & " items</b>. 
                             That is " & TotalPageCount &  " pages</p>"
            mess.Text += "<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"                    
            Dim song As XmlNode 
            For i As Integer = StartRecord to EndRecord
            song = songList.Item(i)
            songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"
        
     Next 
     Else 
           <%--  Do nothing --%>
        End If
    End Sub
</script>
<html>
<head/>
<body>
        <form id="Form1" runat="server">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my collection"
                    runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server" />
        <asp:Label ID="songNodesOut" runat="server" />
        </form>
</body>
</html>

The code is now working, other than that I still have no navigation bars, based on what you had in

Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If

Is there something I need to put in the application to get this to work?
Thanks for your help.

The navigation is really up to you however you want it. You could do several things. If you are never going to have very many pages you can genrate links to each of the pages.

Dim NavigationHTML As String
    
    For i As Integer = 1 To TotalPageCount
        NavigationHTML &= "<a href=""mainPage.aspx?Page=" & i.ToString & """>1</a>&nbsp;"
    Next

You can then place the NavigationHTML after the display of the main table in a Literal control or however you wish.

But if there are going to be alot of pages I would work more on the logic. Look at Google for example at the bottom of a results page. The code for the navigation could get more complex than all the code you have now depending on what you want to do. All this is built into the grid control already. You may want to read some tutorials on customizing the look of the grid if that is your main reason for not wanting to use it.

Kenny

The navigation is really up to you however you want it. You could do several things. If you are never going to have very many pages you can genrate links to each of the pages.

Dim NavigationHTML As String
    
    For i As Integer = 1 To TotalPageCount
        NavigationHTML &= "<a href=""mainPage.aspx?Page=" & i.ToString & """>1</a>&nbsp;"
    Next

You can then place the NavigationHTML after the display of the main table in a Literal control or however you wish.

But if there are going to be alot of pages I would work more on the logic. Look at Google for example at the bottom of a results page. The code for the navigation could get more complex than all the code you have now depending on what you want to do. All this is built into the grid control already. You may want to read some tutorials on customizing the look of the grid if that is your main reason for not wanting to use it.

Kenny

No, the total number of pages that I would need would not exceed more than 5 in most cases. So, it is not like I am going to have 20 pages or more like Google.

Just wondering, is this code something that goes along with

Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If

Of course, I would probably moved it around so that the navigation bar would only appears if the result returning from the drop down selection is available.

Thanks for your help.
Thanks for your help.

First add a Literal control to the HTML where you want the navigation to show up

<asp:Literal ID="navHTML" runat="server"></asp:Literal>

Then somewhere after the calculations add the navigation code I gave you earlier. Right after that code you can populate the navigation html with:

Me.navHTML.Text = NavigationHTML

Does this make sense?

Kenny

First add a Literal control to the HTML where you want the navigation to show up

<asp:Literal ID="navHTML" runat="server"></asp:Literal>

Then somewhere after the calculations add the navigation code I gave you earlier. Right after that code you can populate the navigation html with:

Me.navHTML.Text = NavigationHTML

Does this make sense?

Kenny

Thanks for your promptly response, and I have fiddled around with it, and looks like it can tell which one is the current page, so that page would not have an href link with it.

However, there is still a problem here. Looks like when I click on the link, it tells me the desired page, but then all it gives me is back to the main menu. Is this the problem you were addressing at the beginning of the post?

Oh, and here is the code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>
<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = "" 
        mess.Text = ""

        Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")

        Dim RecordsPerPage As Integer = 10 
        Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
        If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
    
        Dim CurrentPage As Integer = 1
        If Request.QueryString("Page") IsNot Nothing Then
        CurrentPage = CInt(Request.QueryString("Page"))
        End If    
     
       Dim StartRecord As Integer = (CurrentPage-1) * RecordsPerPage + 1
            Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
            If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)    

        If CurrentPage <= TotalPageCount   
            
            songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")            
            mess.Text += "<p>You selected <b>" & drop1.SelectedItem.Text & " " & songList.Count & " items</b>. That is " & TotalPageCount &  " pages</p>"
            mess.Text += "<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"                    
            Dim song As XmlNode 
            For i As Integer = StartRecord to EndRecord
            song = songList.Item(i)
            songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"
        
     Next     
     Else 
           <%--  Do nothing --%>
        End If
   
    Dim NavigationHTML As String = "<center>"
     For j As Integer = 1 To TotalPageCount   
       if j = CurrentPage
        NavigationHTML &= "[" & j & "]"
       else
        NavigationHTML &= "<a href=""test.aspx?Page=" & j.ToString & """>" & j & "</a>&nbsp;"  
       End If
     Next
     NavigationHTML &= "</center>" 
     navHTML.Text = NavigationHTML
	

    End Sub
</script>
<html>
<head/>
<body>
        <form id="Form1" runat="server">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my collection"
                    runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
                <asp:ListItem>Rock</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server" />
        <asp:Label ID="songNodesOut" runat="server" />
        <asp:Literal ID="navHTML" runat="server"></asp:Literal>
        </form>
</body>
</html>

Thanks for your help.

Not sure I follow. Are you saying it is always showing the first "page" (records 1 to 10)?

Not sure I follow. Are you saying it is always showing the first "page" (records 1 to 10)?

Nope, say this is the navigation,

[1] 2 3 4 5

and I select 2 to see page 2 's results, I get this on the browser url: http://localhost/asp_test/test.aspx?Page=2

And, this is what I get in the output, (source code copied from the html output)

<html>
<head/>
<body>
        <form name="Form1" method="post" action="test.aspx?Page=2" id="Form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIwNTI5MDY4NjNkZGSKdnqHxrUmHGMiwtEdFhup5ch3" />
</div>

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBwKehcD1AQLl/JqhBwLZzJ20AwLym56nDQLA14PvBgKOzpCpDQKM54rGBsdE4pwl8U3LxXoj4WoO0kV7ZAN6" />

            <p style="color: red; font-weight: bold">
                <span id="mess">Please make a selection of the type of song list you would like to see from my collection</span></p>

         <select name="drop1" id="drop1">
	<option value="Light Rock">Light Rock</option>
	<option value="Rock">Rock</option>	
            <input type="submit" name="Button1" value="Submit" id="Button1" />
        <span id="songNodesOut"></span>
        </form>
</body>
</html>

So,it is not even technically giving me any result from the first page, but looks like it went back to the point when post is not post back.

What do you think?

I can't get your sample to work on my own server but I believe this is an issue of your combo box. The code I gave you to generate the navigation is generating links directly to the page which I would guess is making you lose your selection. You could change the href on the links to actually perform a post back. Javascript would be required to be enabled on the browser at this point though. Or set the AutoPorstback of your combo to true and store the value of the combo box in a session variable when the onchange event occurs. Does this make sense?

Kenny

I can't get your sample to work on my own server but I believe this is an issue of your combo box. The code I gave you to generate the navigation is generating links directly to the page which I would guess is making you lose your selection. You could change the href on the links to actually perform a post back. Javascript would be required to be enabled on the browser at this point though. Or set the AutoPorstback of your combo to true and store the value of the combo box in a session variable when the onchange event occurs. Does this make sense?

Kenny

Well, I did a quick investigation of AutoPostBack in regards of what examples that are out there. Since my application code is so long, I am just cutting and pasting on what I edited:

<asp:DropDownList ID="drop1" runat="server" AutoPostBack="True">

If I leave it like that, it does not make any change, and I still get Light Rock on my list, with no data.

However, if I remove

<asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server"/>

and change the subroutine declaration from submit to Page_Load, then I do get results, but that is the 2nd page of results in the Light Rock category, which I did not select Light Rock to begin with.

Yet, I found another problem, up on the starting and ending record declaration, you had

If EndRecord > (songList.Count-1) Then EndRecord = (songList.Count-1)

I found that I am missing the last record, so if I have 14 records, the last record I can see is #13. I then changed it to

If EndRecord = songList.Count Then EndRecord = songList.Count

, but then I get object exception errors.

Have I missed something here? Or, would you suggest a different approach?
Thanks for your help.

It may very well be the XmlNodeList object is not zero based as I assumed.

you would want this in that case:

If EndRecord > songList.Count Then EndRecord = songList.Count

I do not want to come across as rude or insulting your inteligence but I feel your experience level may make this task a little difficult to take on. If you could send me the complete source I'll do my best to help out. kdion at kdion dot com

Kenny

Still get

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

No, I understand, but as I am writing this, I am sending you the email to the address you had above. Let me know if you got it.

Got it. I'll let you know.

Thanks,
Kenny

Here is the code I modified and believe it will work as you need now. I've made the following changes:

1) Keep track of the value of the Drop Down with a session variable so that the proper selection is still in place when a navigation link is clicked. Since they do not perform a postback.

2) Added an ACTION property to your form tag. Pointed it to itself to clear the query string upon clicking the submit button. When submitting to a new category it should reset back to page 1

3) Removed and If statement that didn't make much sense to be there.

4) Placed code in the Page_Load event

5) Added Page declaration to top of code and turned on debugging.

Let me know how this works for you.

Thanks,
Kenny

<%@ Page Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
   
       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
       
       	       If IsPostBack Then
       	           Session("DropdownSelection") = drop1.SelectedIndex
       	       Else
       	       	   If Session("DropdownSelection") IsNot Nothing  Then
       	       	   	drop1.SelectedIndex = CInt(Session("DropdownSelection"))
       	       	   End If
       	       End If
       
               songNodesOut.Text = "" 
               mess.Text = ""
       
               Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
               Dim document As XmlDocument = New XmlDocument()
               document.Load(file)
       
               Dim songList As XmlNodeList
               songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
       
               Dim RecordsPerPage As Integer = 10 
               Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
               If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
           
               Dim CurrentPage As Integer = 1
               Dim Topic As String= drop1.SelectedItem.Text
       
               If Request.QueryString("Page") IsNot Nothing Then
                  CurrentPage = CInt(Request.QueryString("Page"))
               End If  
       
            
              Dim StartRecord As Integer = (CurrentPage - 1) * RecordsPerPage + 1
              Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
              If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)
       
                  
                   songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")            
                   mess.Text &= "<p>You selected <b>" & drop1.SelectedItem.Text & " " & songList.Count & " items</b>. That is " & TotalPageCount &  " pages</p>"
                   mess.Text += "<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"      
       
                     
                   Dim song As XmlNode 
                   For i As Integer = StartRecord to EndRecord
                   song = songList.Item(i)
                   songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
                   Dim title As XmlNode = song.FirstChild
                   songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"
       
                   Dim category As XmlNode = title.NextSibling
                   songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
           
                   Dim album As XmlNode = category.NextSibling
                   Dim artist As XmlNode = album.NextSibling
                   songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
                   songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
          
                   Dim date1 As XmlNode = artist.NextSibling
                   Dim dateAttribute As XmlAttribute = date1.Attributes("added")
                   songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>" 
               
            Next 
                     
           
                 
           Dim NavigationHTML As String = "<p style='text-align:center'>"
            For j As Integer = 1 To TotalPageCount   
              if j = CurrentPage
               NavigationHTML &= "[" & j & "] "
              else
               NavigationHTML &= "<a href=""test.aspx?Page=" & j.ToString & """> " & j & "</a>&nbsp;"   
              End If
            Next
            NavigationHTML &= "</p>" 
            navHTML.Text = NavigationHTML

       
       end Sub

   
</script>
<html>
<head/>
<body>
        <form id="Form1" runat="server" action="test.aspx">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my 
collection" runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
                <asp:ListItem>Rock</asp:ListItem>
                <asp:ListItem>Blues</asp:ListItem>
            </asp:DropDownList>
          <asp:Button ID="Button1" Text="Submit" runat="server"/>
        <asp:Label ID="songNodesOut" runat="server" />
        <asp:Literal ID="navHTML" runat="server"></asp:Literal>
        </form>
</body>
</html>

Here is the code I modified and believe it will work as you need now. I've made the following changes:

1) Keep track of the value of the Drop Down with a session variable so that the proper selection is still in place when a navigation link is clicked. Since they do not perform a postback.

2) Added an ACTION property to your form tag. Pointed it to itself to clear the query string upon clicking the submit button. When submitting to a new category it should reset back to page 1

3) Removed and If statement that didn't make much sense to be there.

4) Placed code in the Page_Load event

5) Added Page declaration to top of code and turned on debugging.

Let me know how this works for you.

Thanks,
Kenny

<%@ Page Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
   
       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
       
       	       If IsPostBack Then
       	           Session("DropdownSelection") = drop1.SelectedIndex
       	       Else
       	       	   If Session("DropdownSelection") IsNot Nothing  Then
       	       	   	drop1.SelectedIndex = CInt(Session("DropdownSelection"))
       	       	   End If
       	       End If
       
               songNodesOut.Text = "" 
               mess.Text = ""
       
               Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
               Dim document As XmlDocument = New XmlDocument()
               document.Load(file)
       
               Dim songList As XmlNodeList
               songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
       
               Dim RecordsPerPage As Integer = 10 
               Dim TotalPageCount As Integer = songList.Count \ RecordsPerPage
               If songList.Count Mod RecordsPerPage > 0 Then TotalPageCount += 1
           
               Dim CurrentPage As Integer = 1
               Dim Topic As String= drop1.SelectedItem.Text
       
               If Request.QueryString("Page") IsNot Nothing Then
                  CurrentPage = CInt(Request.QueryString("Page"))
               End If  
       
            
              Dim StartRecord As Integer = (CurrentPage - 1) * RecordsPerPage + 1
              Dim EndRecord As Integer = StartRecord + (RecordsPerPage - 1)
              If EndRecord > (songList.Count - 1) Then EndRecord = (songList.Count - 1)
       
                  
                   songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")            
                   mess.Text &= "<p>You selected <b>" & drop1.SelectedItem.Text & " " & songList.Count & " items</b>. That is " & TotalPageCount &  " pages</p>"
                   mess.Text += "<br />Items for page " & CurrentPage & " is: " & StartRecord & " to " & EndRecord & "<br />"      
       
                     
                   Dim song As XmlNode 
                   For i As Integer = StartRecord to EndRecord
                   song = songList.Item(i)
                   songNodesOut.Text &= "<br /><b>ID:</b> " & i & "<br />"    
                   Dim title As XmlNode = song.FirstChild
                   songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"
       
                   Dim category As XmlNode = title.NextSibling
                   songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
           
                   Dim album As XmlNode = category.NextSibling
                   Dim artist As XmlNode = album.NextSibling
                   songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
                   songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
          
                   Dim date1 As XmlNode = artist.NextSibling
                   Dim dateAttribute As XmlAttribute = date1.Attributes("added")
                   songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>" 
               
            Next 
                     
           
                 
           Dim NavigationHTML As String = "<p style='text-align:center'>"
            For j As Integer = 1 To TotalPageCount   
              if j = CurrentPage
               NavigationHTML &= "[" & j & "] "
              else
               NavigationHTML &= "<a href=""test.aspx?Page=" & j.ToString & """> " & j & "</a>&nbsp;"   
              End If
            Next
            NavigationHTML &= "</p>" 
            navHTML.Text = NavigationHTML

       
       end Sub

   
</script>
<html>
<head/>
<body>
        <form id="Form1" runat="server" action="test.aspx">
            <p style="color: red; font-weight: bold">
                <asp:Label ID="mess" Text="Please make a selection of the type of song list you would like to see from my 
collection" runat="server" /></p>
            <asp:DropDownList ID="drop1" runat="server">
                <asp:ListItem>Light Rock</asp:ListItem>
                <asp:ListItem>Rock</asp:ListItem>
                <asp:ListItem>Blues</asp:ListItem>
            </asp:DropDownList>
          <asp:Button ID="Button1" Text="Submit" runat="server"/>
        <asp:Label ID="songNodesOut" runat="server" />
        <asp:Literal ID="navHTML" runat="server"></asp:Literal>
        </form>
</body>
</html>

Thank you, Kenny.

I did find out that the count variable, by setting one and do songList.Count-1 allowed me to get everything output correctly. I have fixed up the code to the way I wanted, but this application that you have helped me with really got me started.

Cannot wait to continue my learning of ASP.NET.

No problem. I'm glad it worked out. Good luck with your learning. In my opinion the most important aspect of programming is the ability to break down the problem at hand into small enough pieces that you can solve. One step at a time and you'll be fine.

Kenny

commented: Excellent! +10

Kenny>>One step at a time and you'll be fine.

Excellent Kenny!

@ajwei810192 - Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.