如何在ASPX页面中保留值

时间:2011-02-21 05:21:51

标签: asp.net html vb.net

我试图弄清楚如何保留此页面上控件的值(只是用户提交的佣金值和销售区域(东部是10%,西部是20%)。

代码如下:

<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>

我认为它应该是接近的,但是想想也许只是一个小小的错字,有一个“新鲜”眼睛的人能够指出它,因为我已经看了好几个小时了。任何帮助表示赞赏。

提前致谢!

1 个答案:

答案 0 :(得分:5)

这不是asp.net方法。这种方法可以追溯到asp时代。现在,您只需将asp.net控件放在表单标记内,并自动处理回发jazz(基本意图和目的)。 ViewState将在回发时自动保持插入控件的值。

您的代码应该在aspx标记页面上看起来更像这样:

<%@ 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>

然后在我在Show Commission按钮中注册的OnClick回发事件后面的代码中执行你的逻辑:

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