Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting variables from a class that is not the code-behind

Status
Not open for further replies.

wraheem

MIS
Jul 19, 2001
62
US
I recently created a webform that added, updated, and deleted records as needed from a SQL Server 2000 database. After each addition, modification, or deleteion a datagrid and table would be shown with the new data.

After spending some time reading up on test driven development and refactoring I recreated the code using a class libary so that the code could be reused as needed. It runs faster and is much prettier as well. The problem I have is that the code-behind page is calling the class; but I have no idea on how to set the datagrid and tables values from within the class.

My first thought is to return mulitple variables from the class and allow the code-behind to continue setting the table and datagrid values as before, but I'm not really sure on how to return mulitple variables from a class.

It seems that there should be a way to set thoses vars from within the class. I have tried "borrowing" from the code behind by adding imports to the System.Web namespace and haveing it inherit System.Web.UI.Page as well. I have also setup Protected withevents for the table object (I'm tring the table first). It just seems as though the class doesn't "see" or have a "connection" to the page. It errored by saying "Specified argument was out of the range of valid values. Parameter name: index" the code that called it was:

dim strcbcount as string = "test"
Me.table2.Rows(1).Cells(1).Text = strcbcount

The table has been working using the codebehind so there is actually a row 1, cell 1.


I was thinking there should be a namespace I could use sort of like the GetBrowser that is used with the Nunit framework. Something like:

dim page as namespace.GetPage("page.aspx")
page.table1.cells(0).row(0).text = "hello"


I have searched all over the net but I'm afraid that I'm not looking right since this is hard to explain, let alone narrow for a search.

Thanks for your time, patience, any help,

wraheem
 
The class will not see the page you are referencing it from. You will have to pass the objects(datagrids etc.) to a function or sub to be able to work on it. You can also pass the page as an object to the class as well.

Jim
 
Or do it the other way around (that would be my preferred method). Pass whatver data the page needs back to the page via a function in your class and set the UI as necessary.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I thank both of you for your replys but now I'm slightly confused...I took jim's advice, passed the panel (which is not visible on page load) and the table, declared them and passed them to the class. Then in the class I applyed the text to the table cell and made the panel visible. Like so:

In the code-behind,

Dim pnl3 As System.Web.UI.WebControls.Panel = Me.Panel3
Dim tb2 As System.Web.UI.WebControls.Table = Me.Table2
Dim find As New template.Batch 'the class
find.FindAvailBatch(userid, Me.txtCase.Text, "", Me.txtComments1.Text, pnl3, tb2) 'the function in the class


In the class,

tb2.Rows(2).Cells(2).Text = ucount
pnl3.Visible = True

The problem is that nothing shows up. I also tried to pass the entire page so that I can reference different datagrids and panels, etc, without having to send them all.


I also started thinking about ca8msm' suggestion, but I ended back to my original problem of not know what exactly to reference to make the class "talk" to the page.

Please forgive my ignorance on this, but it seems like I'm in the right neighborhood, just knocking on the wrong door.

I defiently don't want anyone to write my code but could one or both of you provide a sample to give me a nudge in the right direction?

Thanks again for your patience.

wraheem
 
Certainly, here's an example in it's simplest form:
Code:
Public Class Class1

    Public ReadOnly Property myValue() As String
        Get
            Return "Hello"
        End Get
    End Property

End Class
Code:
    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
        Dim x As New Class1
        Label1.Text = x.myValue
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top