Hi,

I have a question regarding an issue on submitting a form multiple times. For some reason, my form takes one submit, but then the others, I don't see changes of result. Here is the app:

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

<script runat="server">   

Sub submit(ByVal sender As Object, ByVal e As EventArgs)
              
    mess.Text = "You selected " & drop1.SelectedItem.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 song As XmlNode
    For Each song In songList
    
    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 /><hr />"
    
  Next
        
end Sub
</script>

<html>
<body>
<form runat="server">

<div>
<asp:DropDownList id="drop1" runat="server">
<asp:ListItem>Rock</asp:ListItem>
<asp:ListItem>Blues</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Submit" OnClick="submit" runat="server"/>
</form>

<p><asp:label id="mess" runat="server"/></p>
<asp:Label id="songNodesOut" runat="server" />
</div>
</body>
</html>

In other words, if I first make Rock as my selection from drop down list, the songs are listed correctly, but if I try and select on the Blues, even though those songs exist, I only see the same songs from the other list. Is there something here that I missed?

Thanks for your help.

Dani AI

Generated

The behaviour described is a classic case of preserved control state plus incremental string-building. ASP.NET server controls retain their Text/Value across postbacks, so if you append results each submit you will see previous output reappear. The quick fix that resolved this thread was to clear or overwrite the output control at the start of the submit handler and make sure the Button has an ID so its event is wired correctly — thanks to for confirming that and to for pointing out the XPath angle.

A few practical improvements to make the page more robust:

  • Build the full HTML/result off-line (for example, in a buffer) and set the label once instead of concatenating into the control on every loop. This reduces allocations and makes the output predictable.
  • Be careful with node navigation: using FirstChild/NextSibling can pick up text or whitespace nodes. Use explicit node selection by name (SelectSingleNode or iterating child elements) so the code is not brittle if the XML has formatting or comments.
  • Match your XPath to the actual XML structure. Selecting by a child-element value is different from filtering by an attribute. Confirm whether category is an element or an attribute and use the corresponding XPath predicate.

Longer-term suggestions: bind the XML to a data control (XmlDataSource + Repeater/GridView) or deserialize into objects and data-bind. That separates presentation from XML parsing and avoids manual HTML construction. Also ensure DropDownList population is only done on first load (so user selection is preserved), validate the XML load (file path and exceptions), and add simple logging or breakpoints when debugging.

These adjustments keep each submission idempotent, improve performance, and make the code easier to maintain.

Recommended Answers

All 6 Replies

Hi,

I have a question regarding an issue on submitting a form multiple times. For some reason, my form takes one submit, but then the others, I don't see changes of result. Here is the app:

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

<script runat="server">   

Sub submit(ByVal sender As Object, ByVal e As EventArgs)
              
    mess.Text = "You selected " & drop1.SelectedItem.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 song As XmlNode
    For Each song In songList
    
    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 /><hr />"
    
  Next
        
end Sub
</script>

<html>
<body>
<form runat="server">

<div>
<asp:DropDownList id="drop1" runat="server">
<asp:ListItem>Rock</asp:ListItem>
<asp:ListItem>Blues</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Submit" OnClick="submit" runat="server"/>
</form>

<p><asp:label id="mess" runat="server"/></p>
<asp:Label id="songNodesOut" runat="server" />
</div>
</body>
</html>

In other words, if I first make Rock as my selection from drop down list, the songs are listed correctly, but if I try and select on the Blues, even though those songs exist, I only see the same songs from the other list. Is there something here that I missed?

Thanks for your help.

Just a note,

I don't think my problem here has been solved yet. I have fixed it up and implemented the is Not Page.IsPostBack function into mine, but it does not seem to make a difference.

When I get the first batch of results back, if I want to make another selection, then I am only returned with the first set the second time. Yet, I don't think this is multiple clicking the submit button several times. Here is the code of what I edited,

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

<script runat="server">   
    
    Sub Page_Load()
        If Not Page.IsPostBack Then
            mess.Text = "<p style='color:red;font-weight:bold'>Please make a selection of the type of song list you would like to see from my collection</p>"
        Else
            mess.Text = "<p>You selected " & drop1.SelectedItem.Text & "</p>"
        End If
    End Sub
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
   
        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 song As XmlNode
        For Each song In songList
    
            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/><br />"
   
        Next
		
    End Sub
</script>
<html>
<body>
<form runat="server">
<div>

<p><asp:label id="mess" runat="server"/></p>  
<asp:DropDownList id="drop1" runat="server">
<asp:ListItem>Rock</asp:ListItem>
<asp:ListItem>Blues</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Submit" OnClick="submit" runat="server"/>

</form>
<asp:Label id="songNodesOut" runat="server" />
</div>
</body>
</html>

Anything is appreciated.

Protected Sub submit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        songNodesOut.Text = ""
        Dim file As String = Context.Server.MapPath("~/xml/Music.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        ' songList = document.SelectNodes("/music_songs/song")
        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 &= "<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/><br />"

        Next
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            mess.Text = "<p style='color:red;font-weight:bold'>Please make a selection of the type of song list you would like to see from my collection</p>"
        Else
            mess.Text = "<p>You selected " & drop1.SelectedItem.Text & "</p>"
        End If
    End Sub
Protected Sub submit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        songNodesOut.Text = ""
        Dim file As String = Context.Server.MapPath("~/xml/Music.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)

        Dim songList As XmlNodeList
        ' songList = document.SelectNodes("/music_songs/song")
        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 &= "<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/><br />"

        Next
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            mess.Text = "<p style='color:red;font-weight:bold'>Please make a selection of the type of song list you would like to see from my collection</p>"
        Else
            mess.Text = "<p>You selected " & drop1.SelectedItem.Text & "</p>"
        End If
    End Sub

I guess you were missing part
songList = document.SelectNodes("//music_songs/song[@category='" & drop1.SelectedItem.Text & "']")

No, as a matter of fact, it turned out all I had to do is to add

songNodesOut.Text = String.Empty

after submit has been declared and add a id to asp:button.

Thanks for your replies, a lot of these code snippets really helped me figure this out.

on start of Submit Button Event..
i wrote that already..
seems like u haven;t checked my code..!
songNodesOut.Text = ""

Yes, I must have missed that. This is actually better, thanks!

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.