Saturday, March 31, 2012

separate HTML from codebehind for user control page

I have a site that uses header.ascx as a user control for the Top Menu. I'm trying (to do it the right way) to separate the HTML code from the codebehind but I'm having a difficult time w/ it... I looked at some articles on codebehinds but most of them are event driven like if you click on a button and some event happens... but for my page, I just want to display a menu at a specific location when the form loads... I guess I have to put it in a function and then call it from my main page? But I'm not sure how??
============================
header.ascx
============================
<%@dotnet.itags.org. Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" Inherits="intranet.header" %>

============================
header.ascx.vb
============================
PublicClass nav_header
Inherits System.Web.UI.UserControl
Public strPageTitleAsString
Public strTopMenu1AsString

PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

Response.Write("<title>" & strPageTitle & "</title>")
Response.Write("</head><body>")
Response.Write("<table><tr><td>test...</td></tr>")
Response.Write("<tr><td><table><tr>")

Dim strTopMenu2()AsString
SelectCase strTopMenu1

Case "Header 1"
strTopMenu=NewString() {"Corporate_CatHeader1", "../Corporate/header1.aspx", "Corporate_CatHeader2", "../Corporate/header2.aspx"}
Case "Header 2"
strTopMenu =NewString() {"Practices_CatHeader1", "../Practices/header1.aspx", "Practices_CatHeader2", "../Practices/header2.aspx"}
Case "Header 3"
strTopMenu =NewString() {"WebMail_CatHeader1", "../WebMail/header1.aspx", "WebMail_CatHeader2", "../WebMail/header2.aspx"}
CaseElse' no menu requested, so include a blank spacer

ExitSub
EndSelect

Dim intTopMenu2ItemAsInteger
Dim intSelectedTopMenu1AsInteger
Dim iAsInteger

intSelectedTopMenu1 = (intTopMenu2Item - 1) * 2
For intTopMenu2Item = 0To UBound(strTopMenu2)Step 2
Response.Write("<td> <A href='" & strTopMenu2(intTopMenu2Item + 1) & "'>" & strTopMenu2(intTopMenu2Item) & "</a>")
Response.Write(" | </td>" & vbCrLf)

Next

Response.Write("</tr></table>")
Response.Write("</td></tr></table>")

EndSub

EndClass

In the inherits atribute of your page ensure it matches your class name. In this case your class name is nav_header but the inherits is intranet.header. For starters if you are not using Visual Studio then the intranet part is not necessary unless you wish to encompass the class in a namespace. The quick solution is to change inherits to header and the class name to header.
Sorry I mistyped some of the names... here's a short version.. please help..
===============
header.ascx
===============
<%@. Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" Inherits="myapp.header" %>
<table><tr>
<td>
How can I display the test() here??
</td>
</tr></table>
===============
header.ascx.vb
===============
Public Class header
Inherits System.Web.UI.UserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub test()
Response.Write("test")
End Sub
End Class
Have you tried inherits="header" instead of myapp.header?

0 comments:

Post a Comment