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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing WebForm from Class 1

Status
Not open for further replies.

RobSchultz

Programmer
Jun 1, 2000
444
US
I would like to make a class that will allow me to re-use code among multiple forms. I would like to be able to pass a reference to a form to the class but can't figure out how to accomplish this in ASP.NET (sort of like in old VB you could use ByRef oForm as Form.) Ultimately, I would need to be able to access the controls collection of the webform.

Any ideas? Is there a better way to do this?

Rob

-Focus on the solution to the problem, not the obstacles in the way.-
 
Hey Rob

How about making a base class that derives from System.Web.UI.Page with your reusable functionality in it, and then deriving your pages from that class so you have the functionality available in all pages? That way the controls collection would be directly available in the base class automatically :)

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
After a bit of a nudge from crazyboybert I came up with the following. It doesn't seem very elegant but it does the trick. I would be grateful for any suggestions to make it better.

Create a web form with a few text boxes (txtBox1, txtBox2, etc)
Code:
Public Class TestForm
    Inherits System.Web.UI.Page
    Private oTest As New DesktopExceptions.CommonFunctions


    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

        oTest.TestControl(MyBase.Page, "Form1") 'change the Form1 to the name of your HTML form
    End Sub

End Class



Create a class file...
Code:
Namespace MyProject

    Public Class CommonFunctions

        Public Sub TestControl(ByRef wp As Web.UI.Page, ByVal formName As String)
            Dim wpCTRL As Web.UI.Control = wp.FindControl(formName)
            Dim oCTRL As Control
            Dim oTextBox As TextBox

            For Each oCTRL In wpCTRL.Controls
                Try
                    wp.Response.Write(oCTRL.ID & "<BR>")
                    oCTRL.Visible = False
                Catch
                    wp.Response.Write("ERROR:" & oCTRL.ID & "<BR>")
                End Try
            Next

            'target a single text box
            'oTextBox = wpCTRL.FindControl("txtBox1")
            'oTextBox.Text = "HELLO"

        End Sub
        
    End Class
    
End Namespace

The example shows that you can hide all the controls or target any control by name.

Later,

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top