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
594 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now