将下拉列表选定值传递到其他页面中的详细信息视图?

时间:2022-01-07 13:09:45

I have a table with employee info: email, name , address etc. On one page a have a drop down with only the emails (Primary key) and on the second page I'd like to display a detail view for each employee. So far I've assigned the selected email to a session value.

我有一张包含员工信息的表格:电子邮件,姓名,地址等。在一个页面上有一个下拉列表,只有电子邮件(主键),在第二页面上我想显示每个员工的详细信息视图。到目前为止,我已将所选电子邮件分配给会话值。

    Dim SelectedEmail As String

     SelectedEmail = DropDownList1.SelectedValue
Session("selection") = SelectedEmail

I created a button in the first page that takes me to the detail page and viceversa. I'm having issues retrieving the email, so far i only have this:

我在第一页中创建了一个按钮,将我带到详细页面,反之亦然。我在检索电子邮件时遇到问题,到目前为止我只有这个:

Dim selectedemail As String = Session("selection")

Dim selectedemail As String = Session(“selection”)

2 个解决方案

#1


0  

I'll take a stab.

我会捅一下。

It appears that all you've done is put the selected value in a local variable

您所做的就是将所选值放在局部变量中

Dim SelectedEmail As String
SelectedEmail = DropDownList1.SelectedValue

when I think your intention is to put it in a session variable

当我认为你的意图是把它放在一个会话变量中

Session("selection") = DropDownList1.SelectedValue

So that you can pick it up again on the other page

这样你就可以在另一页上再次拿起它

Dim selectedEmail As String = Session("selection")

#2


0  

I tried to reproduce the issue but it is working fine in my case.

我试图重现这个问题,但它在我的情况下工作正常。

I have added a button and a dropdownlist in a page called default.aspx

我在名为default.aspx的页面中添加了一个按钮和一个下拉列表

 <li class="three">
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
 </li>

In the Page.Load event handler I put three ListItem in the dropDownlist and in the Button.Click event handler I added a session attribute with the selectedValue from the DropDownList

在Page.Load事件处理程序中,我在dropDownlist中放置了三个ListItem,在Button.Click事件处理程序中,我从DropDownList中添加了一个具有selectedValue的会话属性

Here is the code

这是代码

 Public Class _Default
Inherits Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim item1 As ListItem = New ListItem("Test A", "test A", True)
    Dim item2 As ListItem = New ListItem("Test AB", "test AB", True)
    Dim item3 As ListItem = New ListItem("Test C", "test C", True)


    DropDownList1.Items.Add(item1)
    DropDownList1.Items.Add(item2)
    DropDownList1.Items.Add(item3)


End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Session("variable") = "Test value"
    Session("thevalue") = DropDownList1.SelectedValue


    Response.Redirect("default2.aspx")

End Sub

End Class

When the click on the button occurs I provide the SelectedValue to Session("theValue") and redirect to default2.aspx. In default2.aspx I just use the Page.Load event to write on the Response stream after retrieving the Session attribute:

当单击按钮时,我将SelectedValue提供给Session(“theValue”)并重定向到default2.aspx。在default2.aspx中,我只是在检索Session属性后使用Page.Load事件在Response流上写入:

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

    Dim myvariable As String = Session("variable")
    Response.Write("the found value is" + myvariable)
    Dim theValue As String = Session("thevalue")
    Response.Write("<Br/> from drop down =" + theValue)



End Sub

It's working just fine.

它运作得很好。

For example when I select the 2nd item from the DropDownlist I have the following result

例如,当我从DropDownlist中选择第二项时,我得到以下结果

the found value isTest value

找到的值isTest值

from drop down =test AB

从下拉=测试AB

So the issue lies somewhere else. You may check and see if you are handling the IsPostBack correctly in the Page.Load event. If you could share more of your code, that would help.

所以问题出在其他地方。您可以检查并查看是否在Page.Load事件中正确处理IsPostBack。如果您可以共享更多代码,那将有所帮助。

#1


0  

I'll take a stab.

我会捅一下。

It appears that all you've done is put the selected value in a local variable

您所做的就是将所选值放在局部变量中

Dim SelectedEmail As String
SelectedEmail = DropDownList1.SelectedValue

when I think your intention is to put it in a session variable

当我认为你的意图是把它放在一个会话变量中

Session("selection") = DropDownList1.SelectedValue

So that you can pick it up again on the other page

这样你就可以在另一页上再次拿起它

Dim selectedEmail As String = Session("selection")

#2


0  

I tried to reproduce the issue but it is working fine in my case.

我试图重现这个问题,但它在我的情况下工作正常。

I have added a button and a dropdownlist in a page called default.aspx

我在名为default.aspx的页面中添加了一个按钮和一个下拉列表

 <li class="three">
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
 </li>

In the Page.Load event handler I put three ListItem in the dropDownlist and in the Button.Click event handler I added a session attribute with the selectedValue from the DropDownList

在Page.Load事件处理程序中,我在dropDownlist中放置了三个ListItem,在Button.Click事件处理程序中,我从DropDownList中添加了一个具有selectedValue的会话属性

Here is the code

这是代码

 Public Class _Default
Inherits Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim item1 As ListItem = New ListItem("Test A", "test A", True)
    Dim item2 As ListItem = New ListItem("Test AB", "test AB", True)
    Dim item3 As ListItem = New ListItem("Test C", "test C", True)


    DropDownList1.Items.Add(item1)
    DropDownList1.Items.Add(item2)
    DropDownList1.Items.Add(item3)


End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Session("variable") = "Test value"
    Session("thevalue") = DropDownList1.SelectedValue


    Response.Redirect("default2.aspx")

End Sub

End Class

When the click on the button occurs I provide the SelectedValue to Session("theValue") and redirect to default2.aspx. In default2.aspx I just use the Page.Load event to write on the Response stream after retrieving the Session attribute:

当单击按钮时,我将SelectedValue提供给Session(“theValue”)并重定向到default2.aspx。在default2.aspx中,我只是在检索Session属性后使用Page.Load事件在Response流上写入:

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

    Dim myvariable As String = Session("variable")
    Response.Write("the found value is" + myvariable)
    Dim theValue As String = Session("thevalue")
    Response.Write("<Br/> from drop down =" + theValue)



End Sub

It's working just fine.

它运作得很好。

For example when I select the 2nd item from the DropDownlist I have the following result

例如,当我从DropDownlist中选择第二项时,我得到以下结果

the found value isTest value

找到的值isTest值

from drop down =test AB

从下拉=测试AB

So the issue lies somewhere else. You may check and see if you are handling the IsPostBack correctly in the Page.Load event. If you could share more of your code, that would help.

所以问题出在其他地方。您可以检查并查看是否在Page.Load事件中正确处理IsPostBack。如果您可以共享更多代码,那将有所帮助。