首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用VB.net和ASP.net将单选按钮从webform转移到另一个表单

问使用VB.net和ASP.net将单选按钮从webform转移到另一个表单
EN

Stack Overflow用户
提问于 2021-11-06 13:32:05
回答 1查看 35关注 0票数 0

我写了下面的代码,它将单选按钮值和复选框值转移到另一个HTML表单,但我根本没有找到解决方案来将单选按钮或复选框转移到另一个表单,而不仅仅是所选值。

我想要的单选按钮转移到另一种形式,如下面的值。

enter image description here

代码语言:javascript
复制
Dim Gender As String = RadioButton1.SelectedValue
Response.Redirect("PrintPreview.aspx?"&Gender=" + Gender)
Label1.Text = Request.QueryString("Gender")

代码仅返回单选按钮值

请指教

EN

回答 1

Stack Overflow用户

发布于 2021-11-06 15:24:53

好的,第一件事,也是这里最重要的一件事是,当你执行response.Redirect时?

代码语言:javascript
复制
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之后运行的代码。

所以,假设我们有这样的标记:

代码语言:javascript
复制
      <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页面中这样写。

代码语言:javascript
复制
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遍:

代码语言:javascript
复制
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,我们希望获取我们传递的值,并执行以下操作:

代码语言:javascript
复制
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

此外,请注意close您传递给Response.Redirect的字符串的语法也是不正确的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69864434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档