Can you share a code behind file with a page and usercontrol?

Discussion in 'ASP .Net' started by Michael Evanchik, Jul 29, 2003.

  1. Tying not to spaghetti code which seems to be easy to do in .net, im
    trying to do my main .net html in index.aspx, use repeated .net html
    in an .ascx files and all code im doing in .vb code behind files.

    I have no problem using my .vb code behind file for my .aspx pages i
    just have to say <%@ Page Language="vb" Inherits="myCode"
    src="index.vb" %> in my aspx file and use Inherits Page in my .vb code
    behind file and everything is cool

    What do you do for .ascx pages(usercontrols) to share the SAME .vb
    file?? I have tried to add inherits UserControl but only one inherit
    is allowed at a time. Thanks for your help.

    Mike
    Michael Evanchik, Jul 29, 2003
    #1
    1. Advertising

  2. I was able to do it. Im not sure Marina if my question was even clear
    but here is the code I came up with. I can now use the same
    codebehind file for an aspx an ascx just referencing the different
    class names

    codebehind.vb
    -------------------
    Public Class myCode2
    Inherits UserControl
    Public WithEvents clsit As New myCode()

    sub new_agent(sender As Object, e As System.EventArgs)
    dim scalar as string
    scalar = clsit.sql_scalar("select cust_name from customers")
    end sub

    End Class


    Public Class myCode
    Inherits Page
    Function sql_scalar(ByVal str As String) As String
    cmd = New OleDbCommand(str, conn)
    Return cmd.ExecuteScalar()
    End Function
    End Class


    "Marina" <> wrote in message news:<#>...
    > No, you cannot.
    >
    > A .aspx by default inherits from Page, or another descendent of Page. And a
    > user control does the same with UserControl.
    >
    > However, they both inherit from TemplateControl. So, you can try having
    > your one .vb file inherit from TemplateControl instead of either Page or
    > UserControl. Not sure if this will work, especially with the designer.
    > Also, you may have to modify your code, if you are relying on Page or
    > UserControl specific methods or properties.
    >
    > "Michael Evanchik" <> wrote in message
    > news:...
    > > Tying not to spaghetti code which seems to be easy to do in .net, im
    > > trying to do my main .net html in index.aspx, use repeated .net html
    > > in an .ascx files and all code im doing in .vb code behind files.
    > >
    > > I have no problem using my .vb code behind file for my .aspx pages i
    > > just have to say <%@ Page Language="vb" Inherits="myCode"
    > > src="index.vb" %> in my aspx file and use Inherits Page in my .vb code
    > > behind file and everything is cool
    > >
    > > What do you do for .ascx pages(usercontrols) to share the SAME .vb
    > > file?? I have tried to add inherits UserControl but only one inherit
    > > is allowed at a time. Thanks for your help.
    > >
    > > Mike
    Michael Evanchik, Jul 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    565
    Alphonse Giambrone
    Jun 28, 2003
  2. freenews.netfront.net
    Replies:
    2
    Views:
    3,880
    Daniel Fisher\(lennybacon\)
    Nov 14, 2005
  3. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    463
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  4. Replies:
    3
    Views:
    3,671
    Arthur
    Jun 23, 2008
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page