Not able to pass session value from parent page to UserControl. Need someone to check current codes!

Hello,
can someone please take a look of my codes, as not able to execute job base on session value, which should be passed from parent page to user control.

Parent code:
  Public Sub ButtonTest_Click(sender As Object, e As EventArgs) Handles ButtonTest.Click
        Session("OpenDivOrder") = "True" 'Session
    End Sub

Open in new window


UserControl code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim test As String = DirectCast(Session("OpenDivOrder"), String)
        If test = "True" Then
            divTest.Attributes.Add("style", "display: normal;")
        End If
    End Sub

Open in new window

dejandejanovicAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Do you have any Error?

Are you getting the Session value in User Control?
dejandejanovicAuthor Commented:
None error. And, no, I do not get value into UC. For example string Test from message is empty " ".
I'm also not sure how system does execute events. As you can see I have place code from UC into Load event. Maybe UC.Load is fired before parent button, or not?

 Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim test As String = DirectCast(Session("OpenDivOrder"), String)
        MsgBox("message from UC:" + test)
        If test = "True" Then
            divTest.Attributes.Add("style", "display: normal;")
        End If
    End Sub

Open in new window

Lokesh B RDeveloperCommented:
Hi,

As per your first post code.

When you click on button you are assigning the Session.

In UserControl Load() you are Checking for the Session.

When you want the value in User Control?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

dejandejanovicAuthor Commented:
With words when click on parent Button, I want to display hidden DIV element from user control.
Lokesh B RDeveloperCommented:
Hi,

try this code

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
	If Session("OpenDivOrder") IsNot Nothing Then
		Dim test As String = Session("OpenDivOrder").ToString()
		If test = "True" Then
			divTest.Attributes.Add("style", "display: normal;")
		End If
	Else
		Response.Write("Session is NULL")
	End If
End Sub

Open in new window


Public Sub ButtonTest_Click(sender As Object, e As EventArgs)
	'Session
	Session("OpenDivOrder") = "True"
End Sub

Open in new window

dejandejanovicAuthor Commented:
I guess I was need to put event ButtonTest_Click as
<asp:Button ID="ButtonOpenDivOrder" runat="server" OnClick="ButtonTest_Click" />

Open in new window


right, or?
Lokesh B RDeveloperCommented:
Hi,

Here is the solution. You don't need to use Session here.

Use Public Variables.

User Control Code

Public Property IsVisible() As Boolean
	Get
		Return m_IsVisible
	End Get
	Set
		m_IsVisible = Value
	End Set
End Property
Private m_IsVisible As Boolean

Protected Sub Page_Load(sender As Object, e As EventArgs)

End Sub

Protected Overrides Sub OnPreRender(e As EventArgs)
	MyBase.OnPreRender(e)

	If IsVisible Then
		divTest.Attributes.Add("style", "display: normal;")
			' divTest.Attributes.Add("style", "display: normal;");
	Else
	End If
End Sub

Open in new window


Parent Control Button Click code

instead of WebUserControl2, replace your User Control ID
Public Sub ButtonTest_Click(sender As Object, e As EventArgs)
	WebUserControl2.IsVisible = True
End Sub

Open in new window

dejandejanovicAuthor Commented:
Crap. I have totally forgot UC is actual runat = server, and can be simple visible = true/false.

And, for retrieving controls from main page:
Private Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
        TextBoxTest.Text = DirectCast(Me.Parent.FindControl("LabelTest"), Label).Text
    End Sub

Open in new window


Anyway, thank you for help, tips, and direction.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dejandejanovicAuthor Commented:
Feedback very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.