Directory Structure within TreeView VB - asp.net

Started to look at the Treeview control.
Is there anyway to tie the Tree View control into a Directory structure on the Web Server using Visual basic?
I have a lot of legacy files, which are updated and added often. Obviously I could code the structure in XML but this would be laborious and hard to train out to the end user.
I guess it would be a dynamic creation of an XML file perhaps?

Here's an elementary sample that I created awhile ago when learning to play with the TreeView. I have now converted the code to VB.NET using an online converter for your benefit.
It recursively walks the directory tree starting from the root of the virtual directory and creates nodes for each sub-directory or file encountered. I think this is exactly what you needed.
For visual separation, I had used icons to differentiate files from folders (folder.gif and file.gif). You can remove that parameter if you want.
Complete ASPX follows (You can paste it into a new page and it should run):
<%# Page Language="VB" %>
<%# Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim rootDir As New DirectoryInfo(Server.MapPath("~/"))
' Enter the RecurseNodes function to recursively walk the directory tree.
Dim RootNode As TreeNode = RecurseNodes(rootDir)
' Add this Node hierarchy to the TreeNode control.
Treeview1.Nodes.Add(RootNode)
End If
End Sub
Private Function RecurseNodes(ByVal thisDir As DirectoryInfo) As TreeNode
Dim thisDirNode As New TreeNode(thisDir.Name, Nothing, "Images/folder.gif")
' Get all the subdirectories in this Directory.
Dim subDirs As DirectoryInfo() = thisDir.GetDirectories()
For Each subDir As DirectoryInfo In subDirs
thisDirNode.ChildNodes.Add(RecurseNodes(subDir))
Next
' Now get the files in this Directory.
Dim files As FileInfo() = thisDir.GetFiles()
For Each file As FileInfo In files
Dim thisFileNode As New TreeNode(file.Name, Nothing, "Images/file.gif")
thisDirNode.ChildNodes.Add(thisFileNode)
Next
Return thisDirNode
End Function
</script>
<html>
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:treeview ID="Treeview1" runat="server"></asp:treeview>
</form>
</body>
</html>

A custom sitemap provider is a good bet.
There is a good article on 4guys title "Examining ASP.NET 2.0's Site Navigation - Part 4 "

Related

How to use class code of vb.net in asp.net

I'm having trouble with using my vb.net code in asp.net webpage.
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="Banking_Application.WebForm1" CodeFile="~/WebForm1.aspx.vb" %>
class name is WebForm1. I've written code in a separate file and I want to use it in page.
Like
<% Dim total As Integer
Dim val As tests.WebForm1
val = New tests.WebForm1
total = val.TotalBranches()
total.ToString()
%>
I'm new to vb.net and asp.net.
All suggestions are welcome.
Thanks!
Well first of all i need to clarify a few things for you:
in Asp.net, you can embed code in your aspx page using <% %> code block, or write it in a separate file and use it from there.
in your page declaration, you specify the code behind of your page using the CodeBehind= attribute, so if you want to place your code in WebForm1.vb, your page declaration should include CodeBehind="WebForm1.vb" .
note: CodeFile="~/WebForm1.aspx.vb" is not needed.
the structure of the code behind for you aspx page, should look like that:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function test1()
Return "test"
End Function
End Class
and you can add more functions as needed. in the example above, i have added a function called test1 for the purpose of this example.
now, after you have created your code behind in the correct structure, you can call its methods from the aspx page as if it was the same page:
<% =test1()
%>
this will return "test" as specified by the function in code behind.
you do not need to instantiate the class.
I think you are missing what is happening with the mechanics of asp.net.
You either:
Write code directly in the aspx page (within the html)
Or, you wrtie code in the code behind page... your WebForm1.aspx.vb page. From the code behind page, you can access and manipulate the server controls that is on the aspx (html) side...

inline code to populate placeholder VB.NET

I have a problem in so much that I can't find the most upto date version of a site we've created. I have an older version but I can't remember all the changes that have been done. They've requested some changes be done to the site while a new one is in dev. What I'm attempting is below
Page:
<div class="ContentText550">
<asp:placeholder id="phContent" runat="server" />
<% Dim objData As New DAL
objData.Content_DataReader("pagetitle", Page.Header, phContent)%>
</div>
Class:
Public Overloads Sub Content_DataReader(ByVal sPage As String, _
ByVal pHeader As HtmlHead, _
ByVal phContent As PlaceHolder)
<-- Code here to read from database -->
If Not IsDBNull(dr("content_body")) Then phContent.Controls.Add(New LiteralControl(dr("content_body")))
It's not populating the placeholder with anything. Is there a quick solution to this.

Passing any value to FileUpload.FileName using a button click

Despite the firing of the method associated with a form and button click, my fileupload will not pass a value to a string, am I doing something obviously wrong (or just wrong in general)?
Do I need to attach a handler to the fileupload
Here is some sample source, note, it is the only code in the project, I have not made any definitions to the button or fileupload anywhere else:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button_Click()
Dim FileUpload1 As New FileUpload()
Dim X As String = FileUpload1.FileName
Response.Write(X)
End Sub
End Class
and the form:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button_Click" text="Submit"/>
<%-- <input type="file" />--%>
</form>
</body>
</html>
After trying FileUpload.HasFile, it appears as though not only can I not get the file name (described to me in the answer below), but the FileUpload.HasFile is nothing when a file is associated with it as well, is there any reason for this?
Protected Sub Button_Click()
Dim FileUpload1 As New FileUpload()
'Dim X As String = FileUpload1.FileName
'Response.Write(X)
If (FileUpload1.HasFile) Then
' Do Something
' SaveFile(FileUpload1.PostedFile)
Else
End If
End Sub
If you are looking for the path of the uploaded file in the client's machine, that is not allowed for security reasons.
However you should be able to get just the file name using the FileName property.
I check the file name in my applications when i want to test the to see the filetype that is uploaded.
I do not think the following line is required in your Protected Sub Button_Click() function:
Dim FileUpload1 As New FileUpload()
That must be creating a new instance causing it to show you an empty File Name.
If you just need the file name and not the entire path you could try the above.
Edit: Just saw the edit to your questions. The line I asked you to remove may be causing HasFile property to be empty as well.
You can not pass/assign name asp:FileUpload, as it is converted to input type file it is not allowed due to security reason. As it could breach the security of client machine that is browsing the website. The only possibility to assign it a value is through user selection that is browsing and assigning the file by user from client (browser)
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx
You can use FileUpload.SaveAs method to save the selected file.
FileUpload1.SaveAs(savePath);

iTextSharp .pdf tool

Afternoon All,
I have been advised that i can use iTextSharp to help me convert my web pages into .PDF files. I am using the following link as a sample tutorial but cannot generate the .pdf?
Visit http://www.dotnetspark.com/kb/654-simple-way-to-create-pdf-document-using.aspx
I am using the VB sample. I have added the iTextSharp.dll to my project and added the namespaces as requested. I have simply created a blank page and added a button to the page and using the following code i cant seem to get this to generate the file?
Here is my code...
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Partial Class pdf
Inherits System.Web.UI.Page
Protected Sub btnGeneratePDF_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("d:\myfolder\test.pdf", FileMode.Create))
'Open Document to write
doc.Open()
'Write some content
Dim paragraph As New Paragraph("This is my first line using Paragraph.")
Dim pharse As New Phrase("This is my second line using Pharse.")
Dim chunk As New Chunk(" This is my third line using Chunk.")
' Now add the above created text using different class object to our pdf document
doc.Add(paragraph)
doc.Add(pharse)
doc.Add(chunk)
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
doc.Close()
End Try
End Sub
End Class
Here is the code for the button...
<%# Page Language="VB" AutoEventWireup="false" CodeFile="pdf.aspx.vb" Inherits="pdf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" />
</div>
</form>
</body>
</html>
Could someone please take a look at this for me and let me know where i am going wrong?
Regards
Betty
Not sure if this will fix it all, but you're at least missing the Click_Event from your buttton.
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" OnClick="btnGeneratePDF_Click" />
(And I see that you asked this question yesterday: Button ClickEvent is not triggered with the same problem, try to remember next time ;-))

asp.net placeholder in the head of a page

I have a page that includes javascript which I only want to run under certain conditions. To do this I have placed the scripts into an asp:placeholder
This actually seems to work but when I run a debug on the page I get the following warning.
Element 'placeholder' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
If I move the placeholders into the body of the page the warning goes, but that means I'm left with scripts in the body which I also want to avoid. Does anyone have any hints on the best practice for this scenario?? thanks
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
dim lt as new Literal()
lt.text = "<script type='text/javascript' src='scripts/pageLoadAnimations.js'></scr" & "ipt>"
me.Header.Controls.Add(lt)
End Sub
You can include JS file straight from code behind:
If (some condition is true) Then
Page.ClientScript.RegisterClientScriptInclude("jQuery", "jquery-version.js")
End If
A couple of ways which fit your needs are:
Firstly, you could change your <head> tag to <head id="header" runat="server"> then this allows you to dynamically add anything into it, e.g.
dim lt as new Literal()
lt.text = "<script type='text/javascript' src='pathtojavascriptfile'></script>"
me.Header.Controls.Add(lt)
Or you could create a Public string on your page, then stick the javascript in this.
Public _JS as string
Page_Load
_JS = "alert('here');" ' Or what ever your javascript is
ASPX Page
<head>
<script type="text/javascript" src="jquery-version.js"></script>
<script type="text/javascript">
$().ready(function(){
<%=(me._JS) %>
});
</script>
</head>
You might consider looking into the ClientScriptManager. This will allow you to inject scripts into the header properly using whatever conditions you require.
Including Custom Client Script in ASP.NET Pages
ClientScriptManager Class

Resources