How can I use an INCLUDE or USER CONTROL for Page_Load code?

Last post 09-03-2008 10:53 AM by Johnny8675309. 6 replies.

Sort Posts:

  • How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 2:00 PM
    • Member
      1 point Member
    • Johnny8675309
    • Member since 09-02-2008, 1:26 PM
    • Posts 4

    I have a simple (text) data file that I need to load "On Page Load". The HTML section of the page will then display some of the data that is read in. (By the way, I'm using vb with asp.net 1.1 and I'm writing everything in notepad -- I don't use code-behind or Visual Web Developer.)

    However, I need to use that same code into hundreds of pages. If I want to change the code, I want to be able to update a single file -- not hundreds of them. I tried putting the code (which sits between <script runat"server"> and </script> tags) into a User Control -- but it does NOT work. From what I've read, user controls are run/rendered too late in the process -- so user control code in a "Sub Page_Load" does not get run.

    In addition, I don't want the raw code to be visible/included in the pages. In the past, I've had web designers accidentially change code in some pages and I want to prevent that.

    I have a test page done and it works perfectly (as long as the code is hard-coded and saved inside the page). This seems like it would be a VERY common problem -- a simple include or user control SHOULD work, but it doesn't. I've spend hours searching the web and still cannot find an answer.

    Can anyone offer a solution?

    Johnny

    Filed under:
  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 2:35 PM
    • Member
      561 point Member
    • wortho
    • Member since 09-14-2005, 10:10 PM
    • Sunny Northland, New Zealand
    • Posts 88

    Can you post some sample code of this?

    Are you compiling it with the .net compiler before you deploy it?

    If you are not using code behind and you just want to include the file could you put it in JavaScript?

    Using an aspnet usercontrol would be preferable.

    Cheers,

    Steve

    Please remember to 'Mark as Answer' if this post answered your question!
  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 3:07 PM
    • Member
      1 point Member
    • Johnny8675309
    • Member since 09-02-2008, 1:26 PM
    • Posts 4

    Here's a sample of what I'm trying to do. It's a simple problem that I hope has a simple solution!  Wink

     

    <%@Page Explicit="True" Language="VB" Debug="True" %>

    <%@ Import Namespace="System.IO" %>

    <script runat="server">

    Dim SomeData As HashTable = New HashTable

    Sub Page_Load(Sender As Object, E As EventArgs)

    'My code to read a text file into a hashtable will go here.

    'The problem: I want to hide all of the code between the <script> tags in

    'in a single file to make it easy to change and prevent

    'my web designer from accidentially changing the code.

    End Sub

    </script>

     

    <html>

    <head>

    <title>Page Title</title>

    </head>

    <body>

    <!-- The data is displayed here -->

    <%=SomeData("WidgetInfo_1")%>

    </body>

    </html>

  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 3:32 PM
    • Member
      561 point Member
    • wortho
    • Member since 09-14-2005, 10:10 PM
    • Sunny Northland, New Zealand
    • Posts 88

    Ah yeah use the StreamReader to open your text file then while read loop through and add each line to your hashtable (not sure how you index them).

    Or if its just one set of text (dont care how many lines) use StreamReader.ReadToEnd - and you dont need to put it in a hashtable.

    Pop on over to MS for the full story;

    http://msdn.microsoft.com/en-us/library/yw67h925(VS.80).aspx

    hth

    Please remember to 'Mark as Answer' if this post answered your question!
  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 4:16 PM
    • Member
      1 point Member
    • Johnny8675309
    • Member since 09-02-2008, 1:26 PM
    • Posts 4

    wortho:

    Ah yeah use the StreamReader to open your text file then while read loop through and add each line to your hashtable (not sure how you index them).

    Or if its just one set of text (dont care how many lines) use StreamReader.ReadToEnd - and you dont need to put it in a hashtable.

    Pop on over to MS for the full story;

    http://msdn.microsoft.com/en-us/library/yw67h925(VS.80).aspx

    hth

     

    I know how to read files and use hashtables -- that's not the problem. Please read my original post and the bold text in my second post for the problem I'm trying to solve.

    Thanks for the help,

    Johnny

  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-02-2008, 4:36 PM
    Answer
    • Member
      103 point Member
    • ferps
    • Member since 08-25-2008, 2:04 PM
    • Posts 13

    To hide the code for your web designer the best option is using separeted files. Use code behind.

    You will have 2 files, one .ASPX and other .vb

    first, remove script tags, and create another file with the same name of your aspx, but with extension .aspx.vb

     you will nedd to use this diretective in the top of your .aspx file:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="WebForm2"%>

    this article explain everything about Codedehind:
    http://www.asp101.com/articles/john/codebehindnovs/default.asp

    i hope this helps you.

  • Re: How can I use an INCLUDE or USER CONTROL for Page_Load code?

    09-03-2008, 10:53 AM
    • Member
      1 point Member
    • Johnny8675309
    • Member since 09-02-2008, 1:26 PM
    • Posts 4

    Thanks Fernando!

    Using code behind worked perfectly. I had no idea you could use it without Visual Studio.

    Johnny

Page 1 of 1 (7 items)