Solved

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

Posted on 2004-08-05
5
604 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:bullrout
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 11733171
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
 
LVL 8

Expert Comment

by:rfgkev
ID: 11734761
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
 
LVL 5

Author Comment

by:bullrout
ID: 11741032
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
 
LVL 8

Accepted Solution

by:
rfgkev earned 500 total points
ID: 11742191
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
 
LVL 5

Author Comment

by:bullrout
ID: 11770952
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

Featured Post

Independent Software Vendors: 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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 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