【问题标题】:Page wont Load computed ASP WEB variable页面不会加载计算的 ASP WEB 变量
【发布时间】:2021-07-20 12:57:39
【问题描述】:

如何修复此表单,我需要在“按下显示报价”按钮后将结果重定向到不同的页面;这里的主要问题是我无法保存数据并重定向它。第二页应该是这样的:

The price for the vehicle you have configured is [All the options selected/computed]
The offer was issued on [Calendar date1] and remains valid until [Calendar date2]

我已经使用缓存测试了一些东西......仍然没有结果。


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
GetData()
End Sub
1 reference
Private Sub GetData()
lblName.Text = Cache("Name").ToString
1blPrice.Text = Cache("Price").ToString
lblofferDate.Text = Cache("offerDate").ToString
1blofferExpiration.Text = Cache("ExpirationDate").ToString
End Sub

第一页 - 主窗体。

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 204px;
        }
        .auto-style3 {
            text-decoration: underline;
        }
        .auto-style4 {
            width: 843px;
            border-collapse: collapse;
        }
        .auto-style5 {
            height: 24px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label1" runat="server" Text="Last Name:"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label2" runat="server" Text="First Name:"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label3" runat="server" Text="Offer date:"></asp:Label>
                    </td>
                    <td>
                        <asp:Calendar ID="calOfferDate" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
                            <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                            <NextPrevStyle VerticalAlign="Bottom" />
                            <OtherMonthDayStyle ForeColor="#808080" />
                            <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                            <SelectorStyle BackColor="#CCCCCC" />
                            <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                            <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <WeekendDayStyle BackColor="#FFFFCC" />
                        </asp:Calendar>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">
                        <asp:Label ID="Label4" runat="server" Text="Offer expiration date:"></asp:Label>
                    </td>
                    <td>
                        <asp:Calendar ID="calExpirationDate" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
                            <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                            <NextPrevStyle VerticalAlign="Bottom" />
                            <OtherMonthDayStyle ForeColor="#808080" />
                            <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                            <SelectorStyle BackColor="#CCCCCC" />
                            <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                            <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <WeekendDayStyle BackColor="#FFFFCC" />
                        </asp:Calendar>
                    </td>
                </tr>
            </table>
            <hr />

        </div>
        <p class="auto-style3">
            <strong>Set up your car</strong></p>
        <table class="auto-style4">
            <tr>
                <td class="auto-style5">
                    <asp:Label ID="Label5" runat="server" Text="Equipment level"></asp:Label>
                </td>
                <td class="auto-style5">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem Value="20,000">Standard</asp:ListItem>
                        <asp:ListItem Value="22,000">Evolved</asp:ListItem>
                        <asp:ListItem Value="27,000">Advanced</asp:ListItem>
                        <asp:ListItem Value="32,000">Premium</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label6" runat="server" Text="Warranty type"></asp:Label>
                </td>
                <td>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem>Standard (2 years)</asp:ListItem>
                        <asp:ListItem>Extended (5 years)</asp:ListItem>
                    </asp:RadioButtonList>

                     
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label7" runat="server" Text="Optionals"></asp:Label>
                </td>
                <td>
                    <asp:RadioButtonList ID="rblOptionals" runat="server" AutoPostBack="True">
                            <asp:ListItem Selected="True">Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:CheckBoxList ID="cblOptionals" runat="server" RepeatDirection="Horizontal" Width="727px">
                            <asp:ListItem Value="1,000">Alloys wheels</asp:ListItem>
                            <asp:ListItem Value="1,500">Climatronic</asp:ListItem>
                            <asp:ListItem Value="1,300">Automatic pilot</asp:ListItem>
                            <asp:ListItem Value="1,000">Road alert</asp:ListItem>
                            <asp:ListItem Value="850">Navigation</asp:ListItem>
                        </asp:CheckBoxList>
                </td>

            </tr>
            <tr>
                <td class="auto-style5">
                    <asp:Label ID="Label8" runat="server" Text="Discounts"></asp:Label>
                </td>
                <td class="auto-style5"><strong><em>
                    <asp:CheckBoxList ID="cbListDiscounts" runat="server" RepeatDirection="Horizontal" CssClass="auto-style5" Width="394px" TextAlign="Left">
                        <asp:ListItem>Whole payment</asp:ListItem>
                        <asp:ListItem>CASCO insurance</asp:ListItem>
                    </asp:CheckBoxList>
                    </em></strong></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="btnDisplayOffer" runat="server" BackColor="Red" ForeColor="#CCCCCC" Height="20px" Text="Display offer" Width="101px" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

在 FORM 中显示按钮

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnDisplayOffer_Click(sender As Object, e As EventArgs) Handles btnDisplayOffer.Click
        Response.Redirect("Offer.aspx")
    End Sub
    Public Sub CreateCache()
        Cache("Name") = txtFirstName.Text & " " & txtLastName.Text
        Cache("offerDate") = calOfferDate.SelectedDate
        Cache("ExpirationDate") = calExpirationDate.SelectedDate
        Dim price As Integer

        If DropDownList1.Items(0).Selected = True Then
            price = 20000
        ElseIf DropDownList1.Items(1).Selected = True Then
            price = 22000
        ElseIf DropDownList1.Items(2).Selected = True Then
            price = 27000
        Else
            price = 32000
        End If
        If RadioButtonList1.Items(1).Selected = True Then
            price = price + 2000
        End If



        If cbListDiscounts.Items(0).Selected = True Then
            price = price - (price * 0.1)

        End If
        If cbListDiscounts.Items(1).Selected = True Then
            price = price - 1000
        End If

    End Sub

    Protected Sub rblOptionals_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblOptionals.SelectedIndexChanged
        If rblOptionals.SelectedValue.Equals("Yes") Then
            cblOptionals.Visible = True
        Else
            cblOptionals.Visible = False
        End If
    End Sub
End Class

【问题讨论】:

  • 缓存不是正确的选择:它的范围是应用程序范围,这意味着所有用户看到相同的数据。对用户特定数据使用 Session
  • 您的点击处理程序只是重定向而不调用 CreateCache。然后计算价格并忘记它

标签: asp.net vb.net


【解决方案1】:

只需编写您的按钮代码即可。您单击该按钮 - 它会保存一些数据,然后执行简单的响应。重定向。

Protected Sub btnDisplayOffer_Click(sender As Object, e As EventArgs) Handles btnDisplayOffer.Click
    SaveMyData
    Response.Redirect("Offer.aspx")
End Sub

Sub SaveMyData

    Session("Name") = txtFirstName.Text & " " & txtLastName.Text
    Session("offerDate") = calOfferDate.SelectedDate
    Session("ExpirationDate") = calExpirationDate.SelectedDate
    .etc .etc. etc.
End Sub

现在,在目标页面中,您可以在加载时执行以下操作:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    if IsPostBack = False then
        GetData()
    end if
End Sub

Private Sub GetData()
    lblName.Text = Session("Name")
    1blPrice.Text = Session("Price")
    lblofferDate.Text = Session("offerDate")
    1blofferExpiration.Text = Session("ExpirationDate")
End Sub

如前所述,请务必检查 IsPostPack = False。那是您的第一次页面加载-因此该代码块内部就像说 VB6 或 ms-access 表单加载事件一样-第一次是您加载代码的地方-如果 IsPostBack = False,则没有它,那么代码每次都会运行 - 包括您放在表单上的按钮等 - 因此,每次您使用按钮或该页面上的任何事件时,这些控件都会重新设置 - 作为编码习惯 - 让您的页面加载IsPostBack=False 代码存根中的代码 - 不要忘记拥有该代码块 - 养成习惯 - 即使您认为不需要!!!!

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多