好的,在这里要意识到的第一件事,也是最重要的事情是,当您执行 response.Redirect 时?
It STOPS code running in the current page.
No code AFTER the Response.Redirect will run
All variables, and code and ALL values for the current page are destroyed!
因此,您不能(通常)编写代码以在 response.Redirect 之后运行。
所以,假设我们有这个标记:
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Font-Size="Larger" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Done" />
我们的页面现在看起来像这样:
现在,我们要跳转到第 2 页。
所以我们的代码可以说在我们的 Test1 页面中看起来像这样。
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Gender As String = RadioButtonList1.SelectedItem.Text
Response.Redirect("Test2.aspx?&Gender=" & Gender)
' code AFTER above line WILL NOT run!!!!
End Sub
所以,我们不能在 response.Redirect 之后有代码。
再次,阅读 5 遍:
when the response.Redirect is used, then the current page code STOPS,
and ALL values, and even your code variables are DESTROYED!!! This is not much
different then when you get to the end of a subroutine - when you exit, then all
values and things in that subroutine are "gone", and "do not exist".
您的网页也是如此 - 使用 Response.Redirect 意味着停止代码,转移到另一个页面。
所以,现在上面会跳转到页面Test2,我们想取我们传递的那个值,然后这样做:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' save a label on this page to the passed choice
Label1.Text = Request.QueryString("Gender")
End If
End Sub
另外,请注意您在 Response.Redirect 中传递的字符串的语法也不正确。