Access Session value in other page using C# and VB.Net in ASP.Net

Last Reply 2 months ago By arunkurmi

Posted 2 months ago

Hello,

I have the code below that carry from another page, but then somehow only read the first “If statement”, and can’t pass it on to the second "If Statement" if is not true.

Please help.

Page 1:

HTML:

<table>
    <tr>
        <td>
            <p class="padding2">
                <asp:LinkButton ID="IndexAdminComp" runat="server">* Administrative/Computer</asp:LinkButton></p>
        </td>
        <td>
            <asp:Label ID="AdminComplbl" runat="server" Text="1" Font-Size="28px" Font-Names="Arial"
                Visible="false"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <p class="padding2">
                <asp:LinkButton ID="IndexChildYouth" runat="server">* Child/Youth</asp:LinkButton></p>
        </td>
        <td>
            <asp:Label ID="ChildYouthlbl" runat="server" Text="2" Font-Size="28px" Font-Names="Arial"
                Visible="false"></asp:Label>
        </td>
    </tr>
</table>

VB Code:

Protected Sub IndexAdminComp_Click(sender As Object, e As EventArgs) Handles IndexAdminComp.Click
    Response.Redirect("~/Agency/Report/GlobalAgencyTemplate.aspx")
    Session("One") = AdminComplbl.Text
End Sub 

Protected Sub IndexChildYouth_Click(sender As Object, e As EventArgs) Handles IndexChildYouth.Click
    Session("Two") = ChildYouthlbl.Text
    Response.Redirect("~/Agency/Report/GlobalAgencyTemplate.aspx")
End Sub

Page 2:

HTML:

<table>
    <tr>
        <td>
            <p class="padding10">
                <asp:Label ID="AClbl" runat="server" Text="" Font-Size="24px" Font-Bold="true"></asp:Label></p>
        </td>
</table>

VB Code:  here is the place that can't read the second "If"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If AClbl.Text = Session("Two") Then
        GetFieldwork("QChildYouthService_sp")
        AClbl.Text = "CHILD/YOUTH"
    End If
    If AClbl.Text = Session("One") Then
        GetFieldwork("QAdministrativeComputers_sp")
        AClbl.Text = "Adminstrative/Computer"
    End If
End Sub

 

Posted 2 months ago
phonghue says:
 If Session("message") = "One" Then
        lblMessage.Text = "Adminstrative/Computer"
        GetFieldwork("QAdministrativeComputers_sp")
    ElseIf Session("message") = "Two" Then
        lblMessage.Text = "Child/Youth"
        GetFieldwork("QChildYouthService_sp")
    End If

 You have assigned value as 1 and 2 and checking the session value with One and Two. So condition will not be matched.

You need to check with 1 and 2 or change the Label Text with One and Two.


Posted 2 months ago

when i ran the page break on page 2. then 

 

If Session("message") IsNot Nothing Then.  it's mean to me that the "message" need to have something, but then actually that i can't see anything inside.

 

another word, it's empty. or doesn't work.

i am not sure because i put the if statement infront of the session.  

 

i had done many session without the if statement then it's work just fine. 


Posted 2 months ago

hello,

 

well, the second page still can't see the "One" and "Two".  therefore they did not pull any data into the second page.

 

please see the code that you reference me to use. 

HTML 

page1.

 

  <tr>
            <td><p class="padding2"><asp:LinkButton ID="IndexAdminComp" runat="server" OnClick="OnindexAdmin">* Administrative/Computer</asp:LinkButton></p> </td>
            <td><asp:Label ID="AdminComplbl" runat="server" Text="one" Font-Size="28px" Font-Names="Arial" Visible="false"></asp:Label></td> 
        </tr>
        <tr>
            <td><p class="padding2"><asp:LinkButton ID="IndexChildYouth" runat="server" OnClick="OnIndexChild">* Child/Youth</asp:LinkButton></p> </td> 
            <td><asp:Label ID="ChildYouthlbl" runat="server" Text="two" Font-Size="28px" Font-Names="Arial" Visible="false"></asp:Label></td> 
        </tr>

VB

page 1

 Protected Sub OnIndexAdmin(ByVal sender As Object, ByVal e As EventArgs) Handles IndexAdminComp.Click
        Session("message") = AdminComplbl.Text
        Response.Redirect("~/Agency/Report/GlobalAgencyTemplate.aspx")

    End Sub

    Protected Sub OnIndexChild(ByVal sender As Object, ByVal e As EventArgs) Handles IndexChildYouth.Click
        Session("message") = ChildYouthlbl.Text
        Response.Redirect("~/Agency/Report/GlobalAgencyTemplate.aspx")

    End Sub

 

HTML

page2:

 

<td><p class="padding10"><asp:Label ID="lblMessage" runat="server" Text="" Font-Size="24px" Font-Bold="true" ></asp:Label></p></td>

VB:

page2:

 

        If Session("message") IsNot Nothing Then
            If Session("message") = "One" Then
                lblMessage.Text = "Adminstrative/Computer"
                GetFieldwork("QAdministrativeComputers_sp")
            ElseIf Session("message") = "Two" Then
                lblMessage.Text = "Child/Youth"
                GetFieldwork("QChildYouthService_sp")
            End If
        Else
            lblMessage.Text = " "
        End If

 

please help. 


Posted 2 months ago Modified on 2 months ago

Hi phonghue,

On button click you are redirecting before assigning session.

You have to first assign session and then redirect to page.

I have created a sample. Now take its reference and correct your code.

HTML

Page1

<table>
    <tr>
        <td><p class="padding2"><asp:LinkButton ID="lnkIndexAdminComp" runat="server" OnClick="OnIndexAdmin">* Administrative/Computer</asp:LinkButton></p></td>
        <td><asp:Label ID="lblAdmin" runat="server" Text="1" Font-Size="28px" Font-Names="Arial" Visible="false"></asp:Label></td>
    </tr>
    <tr>
        <td><p class="padding2"><asp:LinkButton ID="lnkIndexChildYouth" runat="server" OnClick="OnIndexChild">* Child/Youth</asp:LinkButton></p></td>
        <td><asp:Label ID="lblChild" runat="server" Text="2" Font-Size="28px" Font-Names="Arial" Visible="false"></asp:Label></td>
    </tr>
</table>

Page2

Message : <asp:Label ID="lblMessage" Text="" runat="server" />

Code

C#

Page1

protected void OnIndexAdmin(object sender, EventArgs e)
{
    Session["message"] = lblAdmin.Text;
    Response.Redirect("~/GlobalAgencyTemplate.aspx");
}

protected void OnIndexChild(object sender, EventArgs e)
{
    Session["message"] = lblChild.Text;
    Response.Redirect("~/GlobalAgencyTemplate.aspx");
}

Page2

if (Session["message"] != null)
{
    if (Session["message"].ToString() == "1")
    {
        lblMessage.Text = "Adminstrative/Computer";
    }
    else if (Session["message"].ToString() == "2")
    {
        lblMessage.Text = "CHILD/YOUTH";
    }
}
else
{
    lblMessage.Text = "";
}

VB.Net

Page1

Protected Sub OnIndexAdmin(ByVal sender As Object, ByVal e As EventArgs) Handles lnkIndexAdminComp.Click
    Session("message") = lblAdmin.Text
    Response.Redirect("GlobalAgencyTemplate.aspx")
End Sub

Protected Sub OnIndexChild(ByVal sender As Object, ByVal e As EventArgs) Handles lnkIndexChildYouth.Click
    Session("message") = lblChild.Text
    Response.Redirect("GlobalAgencyTemplate.aspx")
End Sub

Page2

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Session("message") IsNot Nothing Then
        If Session("message") = "One" Then
            lblMessage.Text = "Adminstrative/Computer"
        ElseIf Session("message") = "Two" Then
            lblMessage.Text = "CHILD/YOUTH"
        End If
    Else
        lblMessage.Text = ""
    End If
End Sub

Screenshot


Posted 2 months ago

Hi phonghue,

I will get back to you soon.