Forum Moderators: open
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!
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 PropertyProtected 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" />
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.
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)
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.
I like the "classic ASP mindset". It is simple and intuitive.
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.
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]