|
-
Jan 23rd, 2004, 05:12 AM
#1
Thread Starter
Hyperactive Member
CSS and VB Code
I am using css to format my webpage, but all of the code on the page is displayed via my "code behind". So when I the page runs, the code has already run and my css doesn't seem to format my page correctly.
I did find something that basically allowed me to put code in the code behind, that added HTML to the beginning so it would ensure that my css and javascript files would run correctly.
Hope you can help.
Cheers
Matt
-
Jan 23rd, 2004, 09:38 AM
#2
PowerPoster
OK, there are really two issues here.
First, and you really need to look at this, is that you are creating HTML in your codebehind. There is VERY few reasons to do this, and when you do, you lose the whole benefit of seperation of code and content. Why are you building HTML in your code behind? I can probably show you a different way.
With that said:
You need to include a 'class=' attribute to the html tag you want to format.
So, lets say that you are outputing this string of HTML:
Code:
<b>H</b>ello everyone. I am glad to see you.<br>
You want to format that string based of of your CSS, you would need to do something like this:
Code:
<div class="MyStyle"><b>H</b>ello everyone. I am glad to see you.<br></div>
In your CSS sheet, you need to create a class called MyStyle that it matches up to.
Code:
.MyStyle
{
...Formatting here.....
}
-
Jan 23rd, 2004, 12:06 PM
#3
Thread Starter
Hyperactive Member
I am generating the tables from the codebehind, which I are displaying information from a database. That is the only html code I am writing with in the codebehind.
I have been using :
VB Code:
Response.Write"<td class='mystyle'>" & DataPulledFromTheDatabase & "</td>"
So when the page is displayed it looks shabby, then after a split second the text is formatted, but it is visible to the eye and I wouldn't give that to my dog, nevermind a customer.
VB Code:
.MyStyle ' Will never work
{
...Formatting here.....
}
Body ' Does work
{
...Formatting here.....
}
In the above, the actual 'body' formatting does work, but runs after my 'codebehind ' code has already been processed by the server, thus, the time delay on formatting, whereas the custom one 'MyStyle' has more chance of running than a one legged dog.
Any Ideas?
Regards,
Matt
-
Jan 24th, 2004, 12:44 AM
#4
PowerPoster
Here is the deal, you should be using the table control, not building a table in a html string in your code behind. It isn't that hard to use, and seperates content from code. This is no longer ASP, it is a whole new ball game.
Also, look at the data repeater control and the datagrid control in addition to the table control. These have EVERY bit of functionality you require, you just have to devote a little time in learning them.
If you post the code you are actually using in your code behind, I can help you get started on changing to the asp.net way....
If you want a quick example, I have a sample project I created for another person to show them the basics of the table control. The cool thing about using the controls is when they are rendered, they include all the styling:
http://www.variantx.com/StudyGroupFi...03_Meeting.zip
-
Jan 29th, 2004, 12:20 PM
#5
Thread Starter
Hyperactive Member
First of all thank you for the suggestion.
Here is the code from my codebehind -
VB Code:
Public Class _default1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objFSO, objFolder, objSubFolder, objFile, objFileProp As Object ' Create Object Variables
Dim strURIFolder, strPhysicalFolder, strExtension As String ' Create String Variables
Dim strFile, strFileType, strFileIcon As String ' Create String Variables
Const strImagePath As String = "../images/" ' Create Constant For The Image Directory Path
strURIFolder = Request.ServerVariables("SERVER_NAME") & Mid(Request.CurrentExecutionFilePath, 1, Len(Request.CurrentExecutionFilePath) - Len(Trim(LCase(Right(Request.CurrentExecutionFilePath, Len(Request.CurrentExecutionFilePath) - InStrRev(Request.CurrentExecutionFilePath, "/"))))))
strPhysicalFolder = Mid(Request.PhysicalPath, 1, Len(Request.PhysicalPath) - Len(Trim(LCase(Right(Request.PhysicalPath, Len(Request.PhysicalPath) - InStrRev(Request.PhysicalPath, "\")))))) & "root\"
objFSO = Server.CreateObject("Scripting.FileSystemObject") ' Initialize The File System Object
objFolder = objFSO.GetFolder(strPhysicalFolder) ' Set The Start Folder For The System Object
Response.Write("<table border='1' width'100%' id='tblFolders'>")
Response.Write("<tr>")
Response.Write("<td colspan='3'>Folder Listing</td>")
Response.Write("</tr>")
If Request.QueryString("subfolder") = "" Then ' If QueryString 'subfolder' String Is Empty
If objFolder.Subfolders.Count > 0 Then
Response.Write("<tr>")
Response.Write("<td colspan='2'>Folder Name</td>")
Response.Write("<td> </td>")
Response.Write("</tr>")
For Each objSubFolder In objFolder.Subfolders ' Traverse Through The File System
If Not Left(objSubFolder.Name, 1) = "_" Then ' Use The Left Function To Avoid Interation Through FrontPage Extension Folders
' Display Result On The WebPage
Response.Write("<tr>")
Response.Write("<td colspan='2'><a href='" & "http://" & strURIFolder & "?subfolder=" & objSubFolder.Name & "\" & "'>" & " " & StrConv(objSubFolder.Name, VbStrConv.ProperCase) & "</a></td>")
Response.Write("<td><IMG id=imgFolder name=" & objSubFolder.Name & " alt='" & StrConv(objSubFolder.Name, VbStrConv.ProperCase) & " (Folder)" & "' src='" & strImagePath & "folder16.png' border=0>" & "</td>")
Response.Write("</tr>")
End If ' If Not Left(objSubFolder.Name, 1) = "_" Then
Next ' Find Next File
Else ' If objFolder.Subfolders.Count > 0 Then
End If ' If objFolder.Subfolders.Count > 0 Then
Else ' If Request.QueryString("subfolder") = "" Then
' If The QueryString 'subfolder' Has A Value, Set The objFolder Object To The Full Path
objFolder = objFSO.GetFolder(strPhysicalFolder & Request.QueryString("subfolder"))
If objFolder.Subfolders.Count > 0 Then
Response.Write("<tr>")
Response.Write("<td colspan='2'>Folder Name</td>")
Response.Write("<td> </td>")
Response.Write("</tr>")
For Each objSubFolder In objFolder.Subfolders ' Traverse Through The File System
If Not Left(objSubFolder.Name, 1) = "_" Then ' Use The Left Function To Avoid Interation Through FrontPage Extension Folders
' Display Result On The WebPage
Response.Write("<tr>")
Response.Write("<td colspan='2'><a href='" & "http://" & strURIFolder & "?subfolder=" & Request.QueryString("subfolder") & objSubFolder.Name & "\" & "'>" & " " & StrConv(objSubFolder.Name, VbStrConv.ProperCase) & "</a></td>")
Response.Write("<td><IMG id=imgFolder name=" & objSubFolder.Name & " alt='" & StrConv(objSubFolder.Name, VbStrConv.ProperCase) & " (Folder)" & "' src='" & strImagePath & "folder16.png' border=0>" & "</td>")
Response.Write("</tr>")
End If 'If Not Left(objSubFolder.Name, 1) = "_" Then
Next ' For Each objSubFolder In objFolder.Subfolders
Else ' If objFolder.Subfolders.Count > 0 Then
End If ' If objFolder.Subfolders.Count > 0 Then
End If ' If Request.QueryString("subfolder") = "" Then
Response.Write("<table>")
Response.Write("Files")
For Each objFile In objFolder.Files ' Traverse Through The File System
'Get The Extension Of objFile
strExtension = Trim(LCase(Right(objFile.Path, Len(objFile.Path) - InStrRev(objFile.Path, "."))))
' Avoid Displaying Irrelevant Information
If Not Left(objFile.Name, 1) = "~" And Not Left(objFile.Name, 1) = "_" And Not LCase(objFile.Name) = "default.aspx" Then
' Get The File Name Minus The Extension And Full System Path
strFile = Mid(objFile.Path, InStrRev(objFile.Path, "\") + 1, InStrRev(objFile.Path, ".") - InStrRev(objFile.Path, "\") - 1)
' Convert The File Name To "Proper Case"
StrConv(strFile, VbStrConv.ProperCase)
objFileProp = objFSO.GetFile(objFile.path)
'Start Of Extension Selection
Select Case strExtension
Case "doc"
strFileType = "Word Document"
strFileIcon = "word16.png"
Case "xls"
strFileType = "Excel Document"
strFileIcon = "excel16.png"
Case "pdf"
strFileType = "Adobe Acrobat File"
strFileIcon = "pdf16.png"
Case Else
GoTo InvalidFile
End Select ' Select Case strExtension
End If ' If Not Left(objFile.Name, 1)........
'Display The Results On The Web Page
Response.Write("<IMG id=imgText name=" & strFile & " alt='" & StrConv(strFile, VbStrConv.ProperCase) & " (" & StrConv(strFileType, VbStrConv.ProperCase) & ")" & "' src='" & strImagePath & strFileIcon & "' border=0>" & " ")
Response.Write("<A HREF='" & "http://" & strURIFolder & "root/" & Request.QueryString("subfolder") & (objFile.Name) & "'>" & StrConv(strFile, VbStrConv.ProperCase) & "</A>" & "<BR>")
Response.Write(objFileProp.Size)
InvalidFile:
Next ' For Each objFile In objFolder.Files
' Set Objects To Nothing
objFSO = Nothing
objFolder = Nothing
objFileProp = Nothing
End Sub
End Class
Regards,
Matt
-
Jan 29th, 2004, 03:07 PM
#6
PowerPoster
Your code looks like ASP code that was dropped into .Net. I am not going to be able to spend the time to convert it all, I have other projects. You should really start with a good asp.net book that includes vb.net help in it.
First of all, don't use the File System object anymore...please....
Use the System.IO namespace to do file and folder work.
-
Jan 30th, 2004, 04:05 AM
#7
Thread Starter
Hyperactive Member
I wouldn't expect you to convert the code, I was just looking for suggestions and a start point, and that is what you have given me, so thanks for your help.
-
Jan 31st, 2004, 04:46 PM
#8
PowerPoster
Ok, here is a tip then to help you get started.
ANYWHERE you see HTML code mixed with your vb code, you are doing something wrong. No if's and's or but's about it. Just consider that a rule. I have found no reason yet that you HAVE to have html code in your vb code. I have found it easier on occasion, but that isn't the norm, it is a BIG exception.
Example:
Code:
Response.Write("<IMG id=imgText name=" & strFile & " alt='" & StrConv(strFile, VbStrConv.ProperCase) & " (" & StrConv(strFileType, VbStrConv.ProperCase) & ")" & "' src='" & strImagePath & strFileIcon & "' border=0>" & " ")
Should be turned into an image control on the page, and you just set the properties of the image control to the same things you are doing above. Whats easier to read in code, your code above, or this:
Code:
myimage.Name = strFile
myimage.Text = StrConv(strFile, VbStrConv.ProperCase) & " (" & StrConv(strFileType, VbStrConv.ProperCase) & ")"
'The Text property above is a guess. I can't remember what it is exactly right now.
myimage.Src = strImagePath & strFileIcon
myimage.Border = 0
Better yet, you can set the controls border to 0 with a property in the designer, and limit out that line all together. The name can also be changed out with a common one. Now we are down to 2 lines of easily manageable code....starting to understand? I would much rather go in and fix my version than your big string version.
-
Feb 2nd, 2004, 05:07 AM
#9
Thread Starter
Hyperactive Member
Yeah, I think I got it now, I guess I have to stop thinking in terms of asp and think the .NET way, I got a couple of asp.net and vb.net books over the weekend and I am starting to get the jist of it.
Thanks once again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|