Welcome to WebmasterWorld Guest from 54.144.243.34

Forum Moderators: ocean10000

ASP.Net alternative to <#INCLUDE >

   
3:18 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



I have searched for a long time, and I still can't figure out this one.

Currently, I use <#INCLUDE ...> to include different sections of my page in to the whole. Each include file has a subroutine that is passed variables.

Like this:
main.aspx:

<%@ Page LANGUAGE="VB"%>
<html>
<body>
<%
Dim str1 As String = "String passed to subroutine in included file."
%>
<!--#INCLUDE file="included.aspx"-->
</body>
</html>

included.aspx:

<%
doSomething(str1)
%>

<script runat="server">
Sub doSomething (str2 As String)
Response.write(str2)
End Sub
</script>

What is the (simple) alternative? I think my problem has been being able to still pass the variable to the subroutine or whatever replaces it.

Working or nearly working code would be appreciated. Please don't just say "Use this thing." I won't understand.

Thanks!

5:23 pm on Jun 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I don't have the time now to post code, but you can look into using UserControls.
[netomatix.com...]
7:05 am on Jun 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a simple example:

Test.aspx

<%@ Page Language="VB" %>
<%@ Register src="ucTest.ascx" tagname="ucTest" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:ucTest ID="ucTest1" runat="server" MyBool="true" />
</form>
</body>
</html>

ucTest.ascx

<%@ Control Language="VB" ClassName="ucTest" %>
<script runat="server">
Private _myBool As Boolean
Public Property MyBool() As Boolean
Get
Return _myBool
End Get
Set(ByVal value As Boolean)
_myBool = value
End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Literal1.Text = String.Format("You passed the value: {0}", _myBool.ToString)
End Sub
</script>
<asp:Literal ID="Literal1" runat="server" />

4:25 pm on Jun 4, 2009 (gmt 0)

10+ Year Member



Thank you very much for taking the time Marcel.

Maybe it is because I'm new to it, but it seems awfully complex. You would think there would be a way to pass a variable to a UserControl that was as simple as passing a variable to a subroutine or a function.

11:54 pm on Jun 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



a way to pass a variable to a UserControl

There is, and the example is already in marcel's post. You have a Property available within the control, and you access it in the control's HTML code (or programatically).

marcel's example is MyBool... you set it to whatever you want, and the control will display the results accordingly.

8:51 pm on Jun 9, 2009 (gmt 0)

10+ Year Member



Yes, that is a "a way to pass a variable to a UserControl", but it doesn't look simple to my eyes. Maybe once I'm used to it.

The more I think about this, the more I realize that I want that "include page" to act like a subroutine or a function. If that is what I really want, I might as well include the subroutine in a normal way (maybe in code behind pages(?)) and then just call the subroutine where I want it included.

So where I currently have:
<!--#INCLUDE file="included.aspx"-->

I would have something simple like:
doSomething(str1)

4:54 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...I want that "include page" to act like a subroutine or a function

If you are planning to use these subroutines or functions to output HTML code, then you are stuck in the classic ASP mindset. Could you show us an example of what you are trying to achieve?
5:56 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Yes, I do use subroutines to output HTML.

Basically, the page has many sections: headers, navigation, advertisements, and a few sections for content. Each section currently is a different #INCLUDE page and usually contains a subroutine (like my basic example above).

So each of these sections/subroutines needs to know a few variables - the variables from the search querystring, plus what part of the website the user is in.

Now, I've just taken yet another look at your example. I'm starting to get used to it and understand it. (The nine lines setting up the variables in ucTest.ascx is probably what seemed so complex to me before.)

Here are my questions now:
In a page like ucTest.ascx, do you always use "Sub Page_Load" for the main subroutine?
What does this do: <asp:Literal ID="Literal1" runat="server" />
And, do you suggest some other way of outputting HTML than this model?

I like the "classic ASP mindset". It is simple and intuitive.

6:27 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like the "classic ASP mindset". It is simple and intuitive.

I know what you mean :), but once you've gotten than 'hang' of ASP.Net you'll wonder how you ever managed with ASP.

The nine line setting up the variable (or Property) of the user control are much easier when using C#:

public bool MyBool { get; set; }

But typing them in VB is also fairly easy, just type 'prop' (without the quotes) and then Tab twice, the Property will expand into a sort of 'mini wizard' to help you out. (you are using Visual Studio?)

In a page like ucTest.ascx, do you always use "Sub Page_Load" for the main subroutine?
Yes, this is the one I usually use, sometimes you will need to use pre-init, or pre-render etc. etc..., look into the ASP.Net page lifecycle to learn more about when to use each one.

What does this do: <asp:Literal ID="Literal1" runat="server" />
This is a literal control, you can use it as a Placeholder for output, ie. Literal1.Text = "<h1>Hello World</h1>". If you must create your own HTML output a literal is prefered over using Response.Write.
6:52 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot to add, Yes, I prefer the UserControl Method of generating 'Includes'.

Within the UserControl you have access to the Page Control properties, ie try adding the following line to the user control:

Page.Title = String.Format("You passed the value: {0}", _myBool.ToString)

or even better, add a button to your Default.aspx page and then add this the the user control:

Dim myButton As Button = Page.FindControl("Button1")
If Not myButton Is Nothing Then
myButton.Text = "Found You!"
End If

The user control can also access any querystrings sent to the page, the current URL etc. etc..

Also, another control I make a lot of use of is the MasterPage, this is perfect for headers, menus and footers. Basically it is a User Control on steroids, a great control to use.

[edited by: marcel at 7:10 pm (utc) on June 10, 2009]

7:01 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Great. I'll do a little more research then take a shot at it tomorrow or Friday and update the thread with my experience...
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month