Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sharing values between user controls vb.net

Posted on 2005-03-20
10
Medium Priority
?
303 Views
Last Modified: 2011-10-03
I am newbie and I have a simple question here. I developed a user control1  "partdetail.ascx"  which gets a value from the database and sets that value to a variable.
something as easy as:

*******
Private Counter  As Integer = 0
counter= db.Parameters("@RowCount").Value
*******
No problem here, but
I have another usercontrol "partsummary.ascx"  and I want this 2nd user control to display the value from the "counter" variable that was set in the 1st usercontrol.

How can I do that?

I know that you can create a custom property but I haven't been able to find out how to do it.

any help will be appreciated!!! very urgent! please be as detailed as possible.
0
Comment
Question by:pimplicado
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13589819
in your usercontrol just add

    Public ReadOnly Property Counter() As Integer
        Get
            Return m_counter
        End Get
    End Property

then you should be able to reach it like

textbox1.text = usercontrol1.Counter


0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13590851
Check out different approachs in passing value between ASPX and ASCX pages:

http://www.c-sharpcorner.com/Code/2005/Jan/PassingValuesfrmUCtoASPX.asp

-tushar
0
 

Author Comment

by:pimplicado
ID: 13598883
Thanks for the answer but still this does not make sense... (I know it is something easy but It does not work for me)

Ok

I added the property as indicated

This is what I did...

Class partdetail.ascx
 Private m_counter as Integer = 0   'step 1

 Then In the PageLoad I call getDetail  'step 2

  Public Sub getDetail  'step 3 getDetail sets the value for counter

  m_counter= db.Parameters("@RowCount").Value ' this should be 30
 
 End Sub

Public ReadOnly Property Counter() As Integer ' step 4
        Get
            Return m_counter  
        End Get
    End Property

end class

OK in my 2nd usercontrol

I do the following

class partsummary
 
Dim partCounter As New myUserControl.partdetail

        lblCount.Text = partcounter.Counter

End Class

However this label displays zero or whatever value I set when I do the
Private m_counter as Integer = 0 in the 1st user control.

Why if I changed the value of m_counter in the sub getDetail I only get zero when I try to access this value in my 2nd user control?

I hope I explained myself.

Thanks for your help.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13599160
shouldn't it be

Dim partCounter As New myUserControl.partdetail
        partcounter.getdetail
        lblCount.Text = partcounter.Counter
End Class
0
 

Author Comment

by:pimplicado
ID: 13603501
the gedetail sub expects parameters that are set in the usercontrol.

I just want to be able to use the value of counter in the 2nd user control.
it seems that I am able to get it but as I tried to explain before
when I declare counter as Private m_counter as Integer = 0   this value remains as zero regardless if
I assign a new value let's say m_counter = 30 in a sub inside of the 1st user control

It seems that this property is ignoring  the value that I set in the sub and it is accepting only the zero from the declaration.
what I get in the 2nd user control is zero. or 1 or whatever value I use in the declaration line.
Public ReadOnly Property Counter() As Integer
        Get
            Return m_counter  
        End Get
    End Property

end class

I am not allowed to change that value in the 1st user control??
help please!
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13609274
try it like this

Public Property Counter() As Integer ' step 4
        Get
            Return m_counter  
        End Get
        Set(ByVal Value As Integer)
            m_counter = value
        End Set

    End Property

than set it  from the other

like

usercontrol2.counter =

0
 

Author Comment

by:pimplicado
ID: 13613910
the problem is that I don't need to set the value of counter in usercontrol2
I want to ACCESS the value set in usercontrol1.

Let me change the question...

I have a field called counter in USER CONTROL #1
How can I access the value of counter in USER CONTROL #2?

CLASS USERCONTROL1

page load
getdetail()
end


sub getdetail()
counter = 50
end sub

END CLASS

Now in usercontrol 2
I want to say

CLASS USERCONTROL2
page load
label1.text = counter ' this should be 50
end
END CLASS

0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13619775
Ok it is getting more clear, are these usercontrols the same ?
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 1500 total points
ID: 13619833
Well actually it doesn't matter, What you want is possible but it would make the usercontrol useless in any other form you want to use it in.

this is how it is possible

in usercontrol2 do something like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        label1.Text = DirectCast(Me.Parent, Form2).UserControl11.counter
    End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question