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

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

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

    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 Guest

  2. Similar Questions and Discussions

    1. Getting data from a usercontrol before containing page ends its page load
      How do I load a user control in a web form first, so that the web form's page_load acts according to events on the user control? In my code...
    2. Share common code when developing single Control assemblies?
      Hello: Having trouble getting IDE to play nice/accept when designing Controls that need helper classes... I would like to compile controls as...
    3. Access File Share from ASP.NET using Unmanaged Code
      Hi, We have an application that requires appropriate users to run command files on an adhoc basis. We have implmented a library that uses the...
    4. using a usercontrol from code behind
      I have a test usercontrol, It works fine from the HTML page of the webform. If i want to manipulate the control (properties, methods) I can get...
    5. How to reference UserControl in server code
      I can't seem to figure out how to get a reference to a UserControl in the code-behind for the page that contains the control. All the examples I've...
  3. #2

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

    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" <mzlatkina@hotmail.com> wrote in message news:<#JGlO3hVDHA.3220@tk2msftngp13.phx.gbl>...
    > 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" <mcbain@aol.com> wrote in message
    > news:73446b8b.0307291310.73e024ee@posting.google.c om...
    > > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139