Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Print array list to label

Posted on 2011-10-21
3
Medium Priority
?
398 Views
Last Modified: 2012-08-13
So this is so simple its embarassing. I cannot figure out what the problem is that is causing this. I have a simple calculator that prints the results in a textbox. I then have a button that is supposed to write the result to an arraylist. Then another button that prints the results to a label control. I'm not sure if the arraylist is being populated or not. I try to manually add an item:
resultslist.add("8") but this does not even add to the array as far as I can tell. Please see the code snippet below. Thank You.
<html>
<head runat="server">
    <title></title>
<script runat = "server" language = "VB">
    
    Dim resultslist As New ArrayList
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 + number2
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 - number2
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 * number2
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
      
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        If TextBox2.Text = "0" Then
            MsgBox("Cannot Divide By Zero")

        Else
            Label1.Text = number1 / number2
        End If
    End Sub

    
    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label1.Text = "Result"
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Label1.Text = "Result"
    End Sub

    
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD464 Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="+" Width="16px" />
    <asp:Button ID="Button2" runat="server" Text="-" />
    <asp:Button ID="Button3" runat="server" Text="*" />
    <asp:Button ID="Button4" runat="server" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Enabled="False" Text="Result"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click" 
        Width="101px" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" onclick="Button6_Click" 
        Text="Show Array" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:psychokraft
3 Comments
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 600 total points
ID: 37005929
When you click on button, page is postback and you lost the arraylist data. You can store the listdata either in session or viewstate
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 600 total points
ID: 37005948
Change

   Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub

to


   Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist = Session("results")
        resultslist.Add(Label1.Text.ToString)
        Session("results") = resultslist
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist = Session("results")
        For Each item In resultslist
            Label2.Text = item
        Next
    End Sub



Make sure the resultslist is added to the session in page load

Page_Load
If Not Page.IsPostBack Then
   Session("results") = resultslist
End If
0
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 800 total points
ID: 37006034
Try this
<html>
<head id="Head1" runat="server">
    <title></title>
<script runat = "server" language = "VB">
    
    Dim resultslist As New ArrayList
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Not ViewState.Item("resultslist") Is Nothing Then
                resultslist = ViewState.Item("resultslist")
            End If
        End If
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 + number2
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 - number2
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 * number2
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
      
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        If TextBox2.Text = "0" Then
            MsgBox("Cannot Divide By Zero")

        Else
            Label1.Text = number1 / number2
        End If
    End Sub

    
    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label1.Text = "Result"
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Label1.Text = "Result"
    End Sub

    
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
        ViewState.Item("resultslist") = resultslist
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item In resultslist
            Label2.Text = Label2.Text + item
        Next
    End Sub
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD464 Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="+" Width="16px" />
    <asp:Button ID="Button2" runat="server" Text="-" />
    <asp:Button ID="Button3" runat="server" Text="*" />
    <asp:Button ID="Button4" runat="server" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Enabled="False" Text="Result"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click" 
        Width="101px" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" onclick="Button6_Click" 
        Text="Show Array" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question