VBAHole22 Posted March 1, 2004 Posted March 1, 2004 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? Quote Wanna-Be C# Superstar
VBAHole22 Posted March 1, 2004 Author Posted March 1, 2004 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??? Quote Wanna-Be C# Superstar
kahlua001 Posted March 1, 2004 Posted March 1, 2004 Or use Shared Variables instead of a function to declare them. Quote
VBAHole22 Posted March 1, 2004 Author Posted March 1, 2004 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. Quote Wanna-Be C# Superstar
kahlua001 Posted March 1, 2004 Posted March 1, 2004 Public Class sample Public Shared PresidentEmail As String = "prez@yahoo.com" End Class Then in your aspx <%= myproject.sample.PresidentEmail %> Quote
VBAHole22 Posted March 1, 2004 Author Posted March 1, 2004 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 %> Quote Wanna-Be C# Superstar
*Gurus* Derek Stone Posted March 1, 2004 *Gurus* Posted March 1, 2004 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. Quote Posting Guidelines
VBAHole22 Posted March 2, 2004 Author Posted March 2, 2004 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. Quote Wanna-Be C# Superstar
*Gurus* Derek Stone Posted March 2, 2004 *Gurus* Posted March 2, 2004 Span elements should inherent their parent's attributes. <span id="firstName" runat="server"></span> 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. Quote Posting Guidelines
VBAHole22 Posted March 2, 2004 Author Posted March 2, 2004 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. Quote Wanna-Be C# Superstar
*Gurus* Derek Stone Posted March 3, 2004 *Gurus* Posted March 3, 2004 You may have to cast the span element to a HtmlContainerControl prior to setting it's text. Dim firstnamespan HtmlContainerControl = DirectCast(firstname, HtmlContainerControl) Quote Posting Guidelines
VBAHole22 Posted March 3, 2004 Author Posted March 3, 2004 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. Quote Wanna-Be C# Superstar
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.