Link to home
Start Free TrialLog in
Avatar of dejandejanovic
dejandejanovic

asked on

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

Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

Do you have any Error?

Are you getting the Session value in User Control?
Avatar of dejandejanovic
dejandejanovic

ASKER

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

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?
With words when click on parent Button, I want to display hidden DIV element from user control.
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

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?
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

ASKER CERTIFIED SOLUTION
Avatar of dejandejanovic
dejandejanovic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Feedback very helpful.