How to display files inside subdirectory - c#

I am having difficulty understanding on how to go about displaying images inside sub directories.
Currently I have the following code which goes about displaying the image files within these subdirectories
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> files = new List<string> (Directory.GetFiles(Server.MapPath("/Screenshots/"), "*.png", SearchOption.AllDirectories));
files = files.Select(s => s.Replace(#"D:\wwwroot\blah\blah", "")).ToList();
rptDirectory.DataSource = files;
rptDirectory.DataBind();
}
}
Front end
<asp:Repeater ID="rptDirectory" runat="server">
<HeaderTemplate>
<h2>Results</h2>
<br />
<div style="display: inline;">
</HeaderTemplate>
<ItemTemplate>
<ul>
<li> <asp:HyperLink ID="hplFolder" runat="server" NavigateUrl="<%# Container.DataItem%>" Text="<%# Container.DataItem %>" /></li>
</ul>
</ItemTemplate>
</asp:Repeater>
The above works fine and I am able to display all the images on one page.
However, I want to first display their parent folders first as a hyperlink/link where user then clicks on the link and it take them to another page where all the images for that parent folder will be displayed
Currently the folder structure is as follows
Screenshot (Parent)
Folder1 (Child)
Another Folder (Siblings)
Images
So basically want to Display the Folder 1 name as the hyperlink and then when I click on the folder it takes me to another page where the images are displayed.
Should I use another asp panel to display the images?
Hope I haven't confused what I have said above?
Thanks

You need to use a tree view
Try this http://www.aspsnippets.com/Articles/Display-Directory-Folder-structure-using-ASPNet-TreeView-control-in-C-and-VBNet.aspx

Related

How can I populate a drop down list with web pages in a folder in my web app?

I want to create a drop down that displays all the available we pages with in a specific folder in my web app. So basically I have an Admin page and there I want to have a drop down that displays the web pages in a folder named "Clients". This drop down needs to be updated automatically when a new web page is created within that folder.
This is all I have so far in my code:
<br />
<b>Select Web Page:</b>
<asp:DropDownList ID="Web_Pages" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="WebPage_SelectedIndexChanged">
</asp:DropDownList><br />
<br />
And Code Behind:
protected void WebPage_SelectedIndexChanged(object sender, EventArgs e)
{
// Not sure what to do here to display the pages??
}
Thanks!
You can get the file listing and add them to your dropdown:
string path = Server.MapPath("/");
string[] files = Directory.GetFiles(path, "*.aspx")
.Select(x => Path.GetFileNameWithoutExtension(x));
Web_Pages.Items.AddRange(files);
And this code is actually should go to Page_Load not SelectedIndexChanged.

Access a control's property in an aspx page from another aspx page

I have 2 aspx pages, the aspx page, say page1.aspx contains a list view and the code is as follows
<asp:ListView ID="ListView1" runat="server"
GroupItemCount="3" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table style="table-layout:fixed;width:100%">
<tr id="groupPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td align="center">
<asp:Image ID="productImage" ImageUrl='<%# Eval("ImageUrl") %>' runat="server"/>
<br />
<asp:LinkButton ID="ProductTitleLinkButton"
runat="server" Text='<%# Eval("ProductTitle") %>'
OnClick="ProductTitleLinkButton_Click"
PostBackUrl="~/ItemDetails.aspx">
</asp:LinkButton>
<br />Rs.
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
<br />
</td>
</ItemTemplate>
<GroupSeparatorTemplate>
<tr runat="server">
<td colspan="3"><hr /></td>
</tr>
</GroupSeparatorTemplate>
</asp:ListView>
Here I am try to access the DataSourceId property of ListView1 listview control, ImageUrl property of productImage image control and Text property of ProductTitleLinkButton Link button control from another aspx page.
The code of second aspx page, say page2.aspx is as follows
protected void Page_Load(object sender, EventArgs e)
{
//Checking if itemsView page exists
if (Page.PreviousPage != null)
{
//Getting the list view in previous page
ListView listView_PreviousPage = (ListView)PreviousPage.FindControl("ListView1");
//Getting the data source of list view in the previous page
string dataSource = listView_PreviousPage.DataSourceID;
//Getting the SQL data source used by the list view in the previous page
SqlDataSource sqlDataSource_PreviousPage = (SqlDataSource)PreviousPage.FindControl(dataSource);
//Getting the SelectCommand property (to get the query) of the SQL Data source
string selectCommand = sqlDataSource_PreviousPage.SelectCommand;
//Getting the image of the product selected in itemsView page
Image productImage_PreviousPage = (Image)PreviousPage.FindControl("productImage");
//Getting the image url of the image
string imageUrl_PreviousPage = productImage_PreviousPage.ImageUrl;
}
}
I am using FindControl() to find the control of the previous page. But I am getting an System.NullReferenceException: Object reference not set to an instance of an object. Please help me out. i want the values of the properties of the controls in previous page.
I'm assuming you're clicking from one page to the next. I'd suggest handling the next button click and copying the control values to session() or passing them along in the request for the next page.
Is your page inside a container (master page) ?
If yes, the FindControl method finds controls in the current naming container. If the control you are looking for is inside another control (typically, inside a template), you must first get a reference to the container and then search the container to find the control you want to get.
So, if page1.aspx is contained inside a master page, then get a reference to the master page in page2.aspx like below:
var previousPageMaster = PreviousPage.Master;
Then get a reference to the contentplaceholder in which the listview is contained:
var maincontentplaceholder = previousPageMaster.FindControl("maincontent");
And then you should be able to get the reference to the ListView in the ItemDetails page:
var listView_PreviousPage = (ListView)maincontentplaceholder.FindControl("ListView1");
Check the link for more information,
https://msdn.microsoft.com/en-us/library/ms178139.aspx

Upload multiple files without having to select every time

With HTML5 and ASP.NET 4.5, I created a page that will allow the user to upload multiple files. The ASPX code looks like:
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
<asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />`
c# code:
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
{
string fileName = Path.GetFileName(uploadedFile.FileName);
uploadedFile.SaveAs("C:\\Users\\username\\Desktop\\Uploaded\\" + fileName);
listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
}
}
}
The user selects the files using the FileUpload control and clicks the Upload button and it works great.
My question is: Is is possible to upload files using this method without the user selecting the files? I'd like to have the app always point to a directory on the users hard drive and default to uploading the files in that directory.
The user would see the contents of that directory in a list box and click the upload button or some variation to that.
I don't see any way to add files to the FileUpload control in code.

Open link in a different panel

I have a page that dynamically generated a list of links to a web page (aspx) that opens an image. I do not have direct access to the PDF, only through the ASPX. The code behind is C#.
So if I have a page that has codes that looks like this
<table>
<tr>
<td>
<asp:Panel ID="pnlImageList" runat="server" Visible="true" HorizontalAlign="Center" >
<a href="http://someserver.com/EDocs/View.aspx?application=MB&seqnum=260">
View Scan # 1</a>
</asp:Panel>
</td>
<td>Open Image here</td>
</tr>
</table>
How do I open the view.aspx link in the cell labels 'Open Image Here'??
AHIA,
LarryR...
Use an embedded iframe as shown here:
http://pxd.me/dompdf/www/examples.php
This is essentially doing what you want -- clicking a link (click on any of the "HTML" or "PDF" links) brings up a file in the embedded iframe.

Customized bulleted list items in ASP.NET

I am just a beginner in ASP.NET. My question is simple, I wanna add list items dynamically from the code behind file and I want each item to have a text and couple of images as hyperlinks. The HTML sample should be like,
<ul>
<li>do foo <img src="some_image.png" /></li>
<li>do bar <img src="some_image.png" /></li>
...
</ul>
The number of items is dependent on the collection retrieved by the code behind file.
P.S. my code behind file is written in C#
The Repeater control is the simplest way to create a customized bulleted list, plus it gives you complete control over the HTML you generate. To use it, set up a template like this:
<ul>
<asp:Repeater runat="server" ID="ListRepeater">
<ItemTemplate>
<li>do foo <a href='#'><img src='<%# Eval("ImageSource") %>' /></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
Then in your code-behind (or declaratively in your markup, depending on your preference), set the repeater's data source and bind it:
void Page_Load(object sender, EventArgs e) {
// Some method you've defined to get your images
List<string> imageList = GetImages();
ListRepeater.DataSource = imageList;
ListRepeater.DataBind();
}
ASP.NET renders the template once for each item in your data source.
The Repeater control has more features than what I've shown here, but this should get you started. Good luck!
Edit: a year after writing this answer, I still think repeaters are the best option among server controls, but more and more I prefer foreach statements right in my .aspx templates:
<ul>
<% foreach(Image image in this.Images) { %>
<li>do foo <a href='#'><img src='<%= image.Source %>' /></a></li>
<% } %>
</ul>
Just use the Repeater control. Simply and easy. :)
ASP.Net BulletedList. MSDN

Categories

Resources