Jeff
Jeff

Reputation: 99

How to preserve values in ASPX page

I am trying to figure out how to preserve the values of the controls on this page (just the user-submitted commission value and the sales region (eastern is 10% and western is 20%)).

The code is as follows:

<form id='frmShowCommSingleDocKeepState' action='afShowCommSingleDocKeepState.aspx' method='get' >
<div>

              <%  
                Dim strSale As String
                Dim dblSale As Double
                Dim strRegion As String
                  Dim dblComm As Double
                  Dim E As Double = 0.1
                  Dim W As Double = 0.2

                  strSale = Request.QueryString("txtSale")
                  If strSale = "" Then
                      Response.Write("Monthly Sale")
                      Response.Write("<input id='txtSale' type='text' name='txtSale'/><br /><br />")
                      Response.Write("Select your Sales Region: ")
                      Response.Write("<select id='lstRegion' name='lstRegion' size='2'>")
                      Response.Write("<option value='E'> Eastern</option>")
                      Response.Write("<option value='W'> Western</option>")
                      Response.Write("</select><br /><br />")
                      Response.Write("<input id='btnShowCommission' type='submit' value='Show Commission' /><hr /><br />")

                  Else
                      strRegion = Request.QueryString("lstRegion")
                      dblSale = CType(strSale, Double)
                      Response.Write("Monthly Sale")
                      Response.Write("<input id='txtSale' type='text' name='txtsale' value=  ")
                      Response.Write("'" & strSale & " ' " & "/><br /><br />")
                      Response.Write("Select your Sales Region: ")
                      Response.Write("<select id='lstRegion' name='lstRegion' size='2'>")
                      If strRegion = "E" Then
                          Response.Write("<option value='E' selected='selected'> Eastern</option>")
                      Else
                          Response.Write("<option value='W' selected='selected'>Western</option>")
                      End If


                      Select Case strRegion
                          Case "E"
                              dblComm = dblSale * E
                              Response.Write("Your Commission is: " & FormatCurrency(dblComm))
                          Case "W"
                              dblComm = dblSale * W
                              Response.Write("Your Commission is: " & FormatCurrency(dblComm))
                      End Select
                  End If

                %>
           </div>
          </form>
         </div>
         </div>

        </body>
    </html>

I think it should be close, but thought that perhaps if it was just a small typo that someone with "fresh" eyes would be able to point it out since I've been looking at it for hours now. Any help is appreciated.

Thanks in advance!

Upvotes: 0

Views: 434

Answers (1)

Matthew Cox
Matthew Cox

Reputation: 13682

This is not an asp.net approach. This kind of approach hails back to asp days. Now you simply put your asp.net controls inside of a form tag and the post back jazz is handled automatically (for basic intent and purposes). The ViewState will automatically maintain the values plugged into your controls upon postback.

Your code should look more like this on the aspx markup page:

<%@ Page Title="Home Page" Language="VB" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title>Demo</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <b> Monthly Sale:</b>
            <asp:Label ID="lblSale" runat="server" Text="" />
            <br /><br />
            Sales Region:
            <asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true" >
                <asp:ListItem Text="Eastern" Value="E"></asp:ListItem>
                <asp:ListItem Text="Western" Value="W"></asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Button ID="btnShowCommission" runat="server" Text="Show Commission" />
            <br /><br />
            <asp:Label ID="lblCommission" runat="server" />
        </div>
    </form>
</body>
</html>

And in then do your logic in the code behind during the OnClick post back event I registered in the Show Commission button:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnShowCommission_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowCommission.Click
        //Do work here to change drop down
        //change commission label to show calculated commission on sale
        //change sales label to show whatev =D
    End Sub
End Class

Upvotes: 5

Related Questions