Inherits System.web.ui.page,Inherits System.Web.UI.UserControl in the same class? or is it not possible?

Hi There,

I have a code behind page with a class that populates a listbox control, I am using the control on aspx pages and usercontrols as well. I know that I cannot Inherit a user control and a web page in the same class or can I? The problem is that I need to use a function to populate a dropdownlist that appears sometimes on an aspx page and other times on a user control. I would like to keep all of the functionality in the same code behind and then just reference it in each page, when I try to access a function in a different class it does not work, do I have to rename the function that I'm using the second class  in order for it to work or can I use the inherits clause twice somehow?

Sean - thank in advance for you answer

Public Class CommonFunctions
Inherits System.web.ui.page



  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then

    End If
  End Sub


Public Shared Function
do stuff
End Function

End Class

Public Class CommonFunctionsFormain
Inherits System.Web.UI.UserControl

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then

    End If
  End Sub


Public Shared Function
do stuff
End Function

End Class
LVL 5
bullroutAsked:
Who is Participating?
 
rfgkevConnect With a Mentor Commented:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then
      Dim oCommon As New CommonFunctions
      oCommon.PopulateList(MyDropDownList)
    End If
  End Sub

However if you made it a shared sub

Public NotInheritable Class CommonFunctions
    Public Shared Sub PopulateList(ByRef oControl As DropDownList)
        'DoStuff
    End Sub
End Class


you could just go

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then
      CommonFunctions.PopulateList(MyDropDownList)
    End If
  End Sub
0
 
mmarinovCommented:
Hi,

Why do you have to inherit the Page or UserControl class? You just can create a class that will contains a method with all of the necessary functionality ( you can pass to it as parameters, Page object , source control ( page or usercontrol ) and in the right event in your aspx or ascx file just call this method

actually when you create the file ( aspx or ascx ) you write down which is the class that this file inherits - so if you tell that this is the page file it will acts like a aspx file - if you inherits the one with UserControl it should act as a user control, but i don't know if there is a way to change this programatically

Regards,
B..M
0
 
rfgkevCommented:
Public Class CommonFunctions
    Public Sub PopulateList(ByRef oControl As DropDownList)
        'DoStuff
    End Sub
End Class


Call the function from a page and passit a reference to the dropdownlist, maybe some other parameters aswell to tell it what needs done or some data or something.
0
 
bullroutAuthor Commented:
Hi There,

I am still pretty new to asp .net and I gues still learning how to take advantage of all of it's features. Would I call the function like below if I was to use this on all my pages?

Sean

<%@ Page language="vb" Src="mycodebehind.vb" Inherits="namespace.CommonFunctions" %>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then
      PopulateList(MyDropDownList)
    End If
  End Sub
0
 
bullroutAuthor Commented:
Hi There,

I am actuall using a ( i think ) a differnet way of populating the list, is there anyway that you can post some code so I can have a better understanding of what I can do?

Sean

Public Shared Function BindCategories() as SqlDataReader

          Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
                  Const strSQL as String = "SELECT CategoryID,CategoryDesc AS CategoryDesc " & _
                                                      "FROM tblProductCategories_b ORDER BY CategoryDesc"
          Dim myCommand as New SqlCommand(strSQL, myConnection)
          myConnection.Open()
          Dim objDR1 as SqlDataReader
          objDR1 = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
          Return objDr1
End Function
0
All Courses

From novice to tech pro — start learning today.