?
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
Medium Priority
?
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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