inline code to populate placeholder VB.NET - asp.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.

Related

Transfer radio button from webform to another form using VB.net and ASP.net

I wrote the below code which transfer the radio button value and check box value to another HTML form but i did not find solution to transfer the radio button or check box at all to another form not only the selected value.
I want the radio button to be transfered to another form as below not only the value.
enter image description here
Dim Gender As String = RadioButton1.SelectedValue
Response.Redirect("PrintPreview.aspx?"&Gender=" + Gender)
Label1.Text = Request.QueryString("Gender")
The code only returned the radio button value
Please advise
Ok, the FIRST thing, and MOST important thing to realize here is that when you execute a response.Redirect ?
It STOPS code running in the current page.
No code AFTER the Response.Redirect will run
All variables, and code and ALL values for the current page are destroyed!
So, you can't write (normally) code to run after the response.Redirect.
So, say we have this markup:
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Font-Size="Larger" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Done" />
And our page now looks like this:
Now, we want to jump to page 2.
So our code can say look like this in our Test1 page.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Gender As String = RadioButtonList1.SelectedItem.Text
Response.Redirect("Test2.aspx?&Gender=" & Gender)
' code AFTER above line WILL NOT run!!!!
End Sub
So, we can't have code AFTER the response.Redirect.
Again, read this 5 times:
when the response.Redirect is used, then the current page code STOPS,
and ALL values, and even your code variables are DESTROYED!!! This is not much
different then when you get to the end of a subroutine - when you exit, then all
values and things in that subroutine are "gone", and "do not exist".
The same goes for your web page - using Response.Redirect means STOP code, transfer to another page.
So, now above will jump to page Test2, we want to take that value we passed, and do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' save a label on this page to the passed choice
Label1.Text = Request.QueryString("Gender")
End If
End Sub
Also, note close your syntax for the string you were passing in Response.Redirect was also incorrect.

pass parameters with OnServerClick vb.net

I am really new in VB and It is hard to get my head around it especially working with non-MVC projects, so i wish i can find my answer here.
I want to pass arguments with the OnServerClick the code below is the html code:
<a id="Anchor1" OnServerClick="LogIn" runat=server> Click Me </a>
and the method in the server is below:
Protected Sub LogIn(sender As Object, e As EventArgs)
e.value
End Sub
I want to pass parameters(args) to the LogIn method??
I don't want to use any VB or razor buttons or anchor tags i want to use the html tags with the onserverclick attribute. is that possible??
thank you in advance.
You can add custom attribute to aspx <a> like this.
<a id="Anchor1" OnServerClick="LogIn" runat="server" customdata="hello"> Click Me </a>
And get that value in LogIn.
Protected Sub LogIn(sender As Object, e As EventArgs)
Dim myButton = CType(sender, HtmlAnchor)
Dim valuepassedfromUI = myButton.Attributes("customdata")
Dim buttonID = myButton.ClientID
End Sub
, See the screenshot.

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);

Cache Object and Commit on Page Load

What we want to do is save an entire control into Cache and recommit the properties on page load, including data items. But we'd like the controls to exist already in the page.
Is this possible?
<html>
<asp:Repeater runat="server" id="rptListOfSubscribers">
<ItemTemplate>
<%# Eval("Name")%><br />
</ItemTemplate>
</asp:Repeater>
</html>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Cache("MyRepeater") Is Nothing Then
Dim rpt As Repeater = InitaliseRepeater()
Cache.Insert("MyRepeater"), rpt, Nothing, DateTime.Now.AddMinutes(720), Tim
eSpan.Zero)
End If
rptListOfSubscribers = Cache("MyRepeater")
End Sub
Function InitaliseRepeater() As Repeater
Dim rpt As New Repeater
rpt.DataSource = x
rpt.DataBind()
Return rpt
End Function
Excuse the short-hand code.
Yes that is possible the cache will take any object including a webcontrol. However probably best to do it earlier than Page_Load -Page_init perhaps.
Also I am curious as to your reasons behind doing this .. It seems like an awful solution. Also when you add the control to the page again when the viewstate page cycle methods run it will potentially change the control instance you have...
Can you describe the problem you are trying to solve with the cached web control?

Directory Structure within TreeView VB

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 "

Resources