【问题标题】:Transfer radio button from webform to another form using VB.net and ASP.net使用 VB.net 和 ASP.net 将单选按钮从 web 表单传输到另一个表单
【发布时间】:2021-12-20 04:03:41
【问题描述】:

我编写了以下代码,将单选按钮值和复选框值传输到另一个 HTML 表单,但我没有找到将单选按钮或复选框传输到另一个表单的解决方案,而不仅仅是选定的值。

我希望将单选按钮转移到另一个表单,如下所示,而不仅仅是值。

enter image description here

Dim Gender As String = RadioButton1.SelectedValue
Response.Redirect("PrintPreview.aspx?"&Gender=" + Gender)
Label1.Text = Request.QueryString("Gender")

代码只返回单选按钮值

请指教

【问题讨论】:

  • RadioButton 没有SelectedValue

标签: asp.net vb.net


【解决方案1】:

好的,在这里要意识到的第一件事,也是最重要的事情是,当您执行 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 中传递的字符串的语法也不正确。

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多