SESSION变量未使用数据

时间:2015-12-09 00:51:29

标签: asp.net vb.net

我正在尝试将所选的gridview记录发送到下一页,以使用带数据表的Session填充控件。 我添加了断点,可以看到数据表正在填充,但它似乎没有发送到下一页。请帮助。

第一页VB代码

Protected Sub grdClientlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles grdClientlist.SelectedIndexChanged

    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(20) {New DataColumn("clientid"), New DataColumn("name"), New DataColumn("surname"), New DataColumn("type"), New DataColumn("status"), New DataColumn("assistant"), New DataColumn("companyname"), New DataColumn("telephoneno"), New DataColumn("mobileno"), New DataColumn("faxno"), New DataColumn("email"), New DataColumn("streetaddress"), New DataColumn("streetaddress2"), New DataColumn("streettown"), New DataColumn("streetcountry"), New DataColumn("streetpostalcode"), New DataColumn("postaladdress"), New DataColumn("postaladdress2"), New DataColumn("postaltown"), New DataColumn("postalcountry"), New DataColumn("pospostalcode")})
        dt.Rows.Add(grdClientlist.SelectedRow.Cells(0).Text, grdClientlist.SelectedRow.Cells(1).Text, grdClientlist.SelectedRow.Cells(2).Text, grdClientlist.SelectedRow.Cells(3).Text, grdClientlist.SelectedRow.Cells(4).Text, grdClientlist.SelectedRow.Cells(5).Text, grdClientlist.SelectedRow.Cells(6).Text, grdClientlist.SelectedRow.Cells(7).Text, grdClientlist.SelectedRow.Cells(8).Text, grdClientlist.SelectedRow.Cells(9).Text, grdClientlist.SelectedRow.Cells(10).Text, grdClientlist.SelectedRow.Cells(11).Text, grdClientlist.SelectedRow.Cells(12).Text, grdClientlist.SelectedRow.Cells(13).Text, grdClientlist.SelectedRow.Cells(14).Text, grdClientlist.SelectedRow.Cells(15).Text, grdClientlist.SelectedRow.Cells(16).Text, grdClientlist.SelectedRow.Cells(17).Text, grdClientlist.SelectedRow.Cells(18).Text, grdClientlist.SelectedRow.Cells(19).Text, grdClientlist.SelectedRow.Cells(20).Text)

        Session("client") = dt
        Response.Redirect("ClientDetails.aspx")


    End If


End Sub

Page 2 VB代码

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

    If Session("dt") IsNot Nothing Then
        Dim dtclient As DataTable = New DataTable()
        dtclient = CType(Session("client"), DataTable)

        txtClientNumber.Text = dtclient.Rows.Item("clientid").ToString
        txtName.Text = dtclient.Rows.Item("name").ToString
        txtSurname.Text = dtclient.Rows.Item("surname").ToString
        cboClientType.Text = dtclient.Rows.Item("Type").ToString
        cboClientStatus.Text = dtclient.Rows.Item("status").ToString
        txtAssisstant.Text = dtclient.Rows.Item("assistant").ToString
        txtCompanyName.Text = dtclient.Rows.Item("companyname").ToString
        txtContactTel.Text = dtclient.Rows.Item("telephoneno").ToString
        txtMobileNo.Text = dtclient.Rows.Item("mobileno").ToString
        txtFaxNo.Text = dtclient.Rows.Item("faxno").ToString
        txtEmailAddress.Text = dtclient.Rows.Item("email").ToString
        txtStreetAddress.Text = dtclient.Rows.Item("streetaddress").ToString
        txtStreetAddressL2.Text = dtclient.Rows.Item("streetaddress2").ToString
        txtStreetAddressSuburb.Text = dtclient.Rows.Item("streettown").ToString
        txtStreetCountry.Text = dtclient.Rows.Item("streetcountry").ToString
        txtStreetPostalCode.Text = dtclient.Rows.Item("streetpostalcode").ToString
        txtPostalAddress.Text = dtclient.Rows.Item("postaladdress").ToString
        txtPostalAddressL2.Text = dtclient.Rows.Item("postaladdress2").ToString
        txtPostalAddressSuburb.Text = dtclient.Rows.Item("postaltown").ToString
        txtPostalCountry.Text = dtclient.Rows.Item("postalcountry").ToString
        txtPostalPostalCode.Text = dtclient.Rows.Item("pospostalcode").ToString
    End If

End Sub

我尝试对行进行索引,也没有为行编制索引相同的结果。

2 个答案:

答案 0 :(得分:1)

If Session("dt") IsNot Nothing Then

将此行更改为以下内容:

If Session("client") IsNot Nothing Then

答案 1 :(得分:0)

第二页的代码通过错误的名称引用会话变量。在您的第一页中,您可以:

Session("client") = dt

这会将DataTable dt分配给会话变量client。回到第二页的这些数据(正如已经指出的那样),您需要引用Session("client"),这就是现在保存数据的内容。浏览器完成渲染第一页后,DataTable类型的原始变量dt就被销毁了。