replacing asp include directive

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
I am converting an asp site to asp.net. I had one page that had a lot of email links for officers in a company. When I built the asp page rather than hard code the email address in the html I kept them in an include page and just included that on any page that needed an address. This made it easy when someone got promoted or left the company because I only had to change in one place.

So how do I emulate this in asp.net?

I created a VB class and put the variables in it. But how can I include this in my html page directive and how can I reference the variable within some html?
 
I don't really need a UI element. All I have is 6 string variables that I want to keep centralized and reference in my code.

Here is my class


Namespace functions
Public Class MailList

variables......

End Class
End Namespace


And then in my aspx page I have:

<%@ Import Namespace="Functions.MailList.vb" %>

in the directives at the top and then in my page I have:

<a href="mailto:" & <%=Functions.MailList.Prez()%> & "?Subject=stuff">President@yoyo.org</a>


I noticed that it put the () after my Prez. I guess it wants to see a function name???
 
I really don't have any functions. They are merely variables that don't change and I don't need to manipulate them. I have added them as read only properties of my class but I can't get them to import into the asp.net page for some reason.
 
Public Class sample
Public Shared PresidentEmail As String = "prez@yahoo.com"
End Class


Then in your aspx

<%= myproject.sample.PresidentEmail %>
 
Thank you so much for being patient kahlua001.

I got it hammered out. I had a namespace in there too so I had to modify your last line a tad

<%= myproject.mynamespace.sample.PresidentEmail %>
 
Ideally you should look towards setting the UI elements to these variables in the "Page_Load" event instead of placing sporadic calls to Response.Write() throughout the page. In doing so you'll generate a more maintainable Web site.
 
I couldn't agree with you more, Derek, that containing variables in the code behind page on the On_Load event is much more feasible and makes for a more efficient design. But in this case how would I go about doing that?

I have text in a span tag that I want to be inline with, and styled like, the text that gets generated from the variable call. I figured out how i could assign the variable to a label in the On_Load event and it seems easy enough. I just envision a web page where all of the text looks consistent except for these variables. It kind of detracts from their dynamic nature.
 
Span elements should inherent their parent's attributes.
Code:
<span id="firstName" runat="server"></span>
Code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     firstName.InnerText = yourVariable
End Sub
You may have to cast the span element to a HtmlContainerControl prior to setting it's text.
 
Thanks Derek,

VS.NET is telling me that I can't use the span tag because I am inside a table.

Also the element isn't being recognized in the code behind page. I can't reference it as

firstname

or as Me.firstname

The cast won't accept it either.

But it does sound like this should work.
 
I finally hammered it out!! Thanks for your help Derek.

Here is what I have for html:

<td><span id="ctlPresidentElect" runat="server"></span></TD></TR>

What I was missing was a declaration in the 'Windows Generated' section like so:

Protected WithEvents ctlPresidentElect As HtmlGenericControl

And then in Page_Load:

ctlPresidentElect.InnerHtml = "<A href=" & "mailto:" & PrezElect & "?Subject=SureNough" & " >PresidentElect@dotnot.org</A>"

Thanks again, I appreciate it.
 
Back
Top