User class objects

SteveoAtilla

Regular
Joined
Dec 22, 2004
Messages
80
Location
The Frozen Tundra
Hey All!

I have created a class object (.ascx file) and am testing the functionality of it.

I am up to the point of testing the different elements of the class, and I have run into a wall that I would not have anticipated.

The class object has (of course) various controls and objects. I was simply trying to set the value of lblError.Text from the page with the user control.

I have the class built and names "ProdSearch".

What type of statement would I use? Here is what I was trying:

Code:
    Dim SearchPanel As New ProdSearch
...
    SearchPanel.lblError.Text = "Test Text"

But I don't get the 'lblError' as an object after I type "SearchPanel."

What am I doing wrong?

Thanks,

Kahuna
 
Thanks for the quick reply.

I now have a public property ("ErrText"), but I still don't understand how to, within the class, force that value to the .Text property of the label "lblError".

Thanks again,

Kahuna
 
Code:
Private _strError As String

Public WriteOnly Property Error() As String
   Set(ByVal Value As String)
      _strError = Value
   End Set
End Property

Public Function Set_Params()
   'Do work
   lblError.Text = _strError
End Function


-----
Dim myUserControl As new MyUserControl
myUserControl.Error = "Testing Error"
myUserControl.Set_Params()

OR

do your work in the Page_Load of the user control
Code:
Private Sub Page_Load(byval s as object, byval e as eventargs) handles mybase.load
   lblError.Text = _strError
End Sub
 
SteveoAtilla said:
That's the missing piece.

Thanks Kahlua!

I spoke too soon. I get the dreaded "Object reference not set to an instance of an object."

Here's my class code:

Code:
Public Class ProdSearch

    Private ErrorText As String = " "

    Public WriteOnly Property ErrText() As String
        Set(ByVal Value As String)
            ErrorText = Value
        End Set
    End Property

    Public Function Set_Params()
        'Do some work!
        lblError.Text = ErrorText
    End Function

And here's how I try to initiate it:

Code:
Public Class ClassTest

    Dim SearchPanel As New ProdSearch

     '... 

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        SearchPanel.ErrText = "This is text from a button outside the class!"
        SearchPanel.Set_Params()
    End Sub

How do I mess something this simple up???

Kahuna
 
Sorry my bad, I got confused for a second on whether this was a class or a user control.

When you declare the user control, do it like this
Code:
Protected WithEvents WebUserControl11 As myproject.WebUserControl1
This is how it looks in the aspx page
Code:
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>

Then you can reference like
Code:
WebUserControl11.ErrorText = "testing"
 
kahlua001 said:
a user control inherits from the System.Web.UI.UserControl class and has a visual interface

Okay. So where do I put the "Protected Withevents" line? Here is the top of the .ascx file I have now:

Code:
Imports System.Data.SqlClient
Public Class ProdSearch
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
...
#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

Thanks for all the help...

Kahuna
 
Back
Top