Add link buttons to each file uploaded using File upload control

Hi,

  In my application I have one file upload control. When the user selectes a file to upload and when he clicks on Attach button I need to show him the file name of the file he selected to upload and also show the Remove link button after the file name. In this case I need to generate link button dynamically.

I wrote a code like the following:

protected void btnAttach_ServerClick(object sender, EventArgs e)

{

string strFileName = MyFile.PostedFile.FileName;string c = System.IO.Path.GetFileName(strFileName);

listBoxAttachedFiles.Items.Add(c);

LinkButton b = new LinkButton();

b.Text = "Remove";

b.ID = "LB_" + b.Text;b.Style.Add("display", "block");

listBoxLinks.Controls.Add(b);

//Panel1.Controls.Add(b);

}

With the above code it is showing only th file name in the listbox it is not showing the Remove button in the listboxlinks list box.

How to generate the link button and and write event when the remove is clicked i have to remove the corresponding  file name from the other listbox.

Thanks in advance.

0
kalkumar
7/9/2007 4:57:22 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
1123 Views

Similar Articles

[PageSpeed] 18

Hey Kalkumar,

 

Here is some code i wrote to do something similar, hope it is of help:

 

<form id="form1" runat="server">
            <asp:Repeater ID="fileRepeater" runat="server" OnItemDataBound="fileRepeater_ItemDataBound">
                <HeaderTemplate>
                    <table>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="fileNameLable" runat="server"/> <asp:LinkButton ID="removeLinkButton" runat="server" Text="Remove" OnCommand="removeLinkButton_Click"/>
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
            <hr />
            <asp:FileUpload ID="uploadFile" runat="server" />
            <asp:Button id="uploadButton" Text="Upload File" OnClick="uploadButton_Click" runat="server" Width="105px" />

 ___________________________________________________________________________________________________________________

 

        protected void Page_Load(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("/images"));
            FileInfo[] rgFiles = di.GetFiles("*.*");

            fileRepeater.DataSource = rgFiles;
            fileRepeater.DataBind();
        }

        protected void fileRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                FileInfo fi = (FileInfo)e.Item.DataItem;
                Label fileNameLable = (Label) e.Item.FindControl("fileNameLable");
                LinkButton removeLinkButton = (LinkButton)e.Item.FindControl("removeLinkButton");

                fileNameLable.Text = fi.Name;
                removeLinkButton.CommandArgument = fi.Name;
            }
           
        }

        protected void removeLinkButton_Click(object sender, CommandEventArgs e)
        {
            FileInfo fi = new FileInfo(Server.MapPath("/images/" + e.CommandArgument.ToString()));
            fi.Delete();

            DirectoryInfo di = new DirectoryInfo(Server.MapPath("/images"));
            FileInfo[] rgFiles = di.GetFiles("*.*");

            fileRepeater.DataSource = rgFiles;
            fileRepeater.DataBind();
        }

        protected void uploadButton_Click(object sender, EventArgs e)
        {
            uploadFile.SaveAs(Server.MapPath("/images/" + uploadFile.FileName));

            DirectoryInfo di = new DirectoryInfo(Server.MapPath("/images"));
            FileInfo[] rgFiles = di.GetFiles("*.*");

            fileRepeater.DataSource = rgFiles;
            fileRepeater.DataBind();
        }

You will need to include "System.IO;"

 

Aaron

 


Please click "Mark as Answer" if my reply solved your problem ;)

"Don't worry I code better when I am pissed"
0
gravypower
7/9/2007 11:21:43 PM

Hi kalkumar:

When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler. Otherwise, the controls may not behave as expected.

About how to dynamically create controls, please refer to the page:http://support.microsoft.com/kb/317515/en-us.

But I don't think it is a good choice to add the remove button dynamically.

You'd better add statically and make it invisible if there is no file uploaded. After the file is uploaded, you just make it visible by modifying the Visible property of the Button.

Please have a try.

If you have any problems about it, please feel free to post back.


Sincerely,
Techie Zhang
Microsoft Online Community Support
0
Techie
7/11/2007 7:57:59 AM
Reply:

Similar Artilces:

How to identify the original file type and restrict the file uploading while using file upload control
 Hi asp.net , I am uploading a file using FileUpload control in asp.net using vb.net.I wrote the code to upload only PDF files. the user can change the extension and change to PDF . I have to check is the file original PDF or not then i have to upload the PDF files only . How to do this ??  Hi, Check the file Mime type for "application/pdf" by looking at the:  FileUpload1.PostedFile.ContentType RegardsSmcoxonNo Gem is ever polished without some friction. Hi smcoxon , Thanks For Replying ,But i found that there is no use using PostedFil...

How Do I Enable A File Upload Control When Another File Upload Control Has A Value?
Hi There,Thanks you for taking the time to read this post.  I am quite new to programming and have searched the net and experimented with my own code but not been able to get it to work yet.I am creating a simple CMS for a friends site and my friend wants to be able to upload 9 images per page.I have created an asp.net form with 9 file upload controls.  I want 1 control to be enabled by default and the other 8 controls disabled until the previous control has a value.  I have set the default setting in visual studio without any problem.My first thought was to double cli...

Is there any limitation of file types that file upload control can be used to upload?
I am working on a content management system which requires me to upload WebEx recording files (.wrf) through file upload control. But I am getting "internet explorer cannot display the webpage" error. It worked with other files types as txt, doc, xls, pdf, jpg, gif etc. Is it related to any security issue or just a bug with the file upload control?  Thank you. The file upload control doesn't have any file type limitations that I'm aware of, but it does have some size constraints.  By default, ASP.NET is configured to not accept any file's larger than 4...

File Upload control-Control file size
I am using a file upload tool. I believe the default permissible size for a file uploaded through this tool is around 4 mb. How do I change this limit? I don't want it to be changed for all upload controls on the page. I just want it to be changed for one particular control which uploads music. I want to raise the uploaded file size limit for music to 10MB. hi, here are some information for you to refer to, http://www.vikramlakhotia.com/Dealing_With_the_ASPNet_Upload_File_Size_Problem.aspx http://fileup.softartisans.com/fileup-242.aspx http://www.codeproject.com/useritems/AJAXUpload.as...

Upload all files in folder with file upload control
H i I'm trying to upload all files to a wesite that is in a specific local folder. My code workes fine when I run it locally but when I upload it it fails. I think it looks for the files to upload on the web server and thus cannot find it.  1 Dim files() As String 2 folderExists = System.IO.Directory.Exists(foldername) 3 If folderExists = False Then 4 Label1.Text = "Folder does not exist. Did you enter a folder name or just a file name ?" 5 Exit sub 6 End If 7 files = System.IO.Directory.GetFiles(foldername) 8 For...

Deleting A File Uploaded Through File Upload Control
Hello, I have a form which allows a user to upload a file to a virtual drive on our server using the following code: fileInvoice.SaveAs(ConfigurationManager.AppSettings.Get("DocumentPath") + strFileName); //Document Path is defined in the web.config file How can I safely allow a form on a web page to delete the file if they know the exact name of the file. I don't want them to be able to do a wildcard match like test* thanks in advance, mitch  Peace be on you brother, so you want to safely delete the file which the name you know. OK I consider that the name ...

Posting filepath in file upload control in .NET to the next page and uploading the file from second page
In our application (using C# .NET) there is a form with file upload options. After filling up the form details, the summary of the details need to be shown in the next page before committing the information into the database. Right now, Server.Control is used to transfer the contents to the next page and the form details are retrieved using Request.Form["control id"]. But this is not working for File upload control alone.File upload can not be done in the first page itself, as the foreign key in File upload table gets updated only in second page after confirmation. Therefore, is th...

how to get author from file when upload using upload control using javascript
function CheckSchemaFileExistence() { var Schemafilename=document.getElementById('<%= this.flUploadB.ClientID %>').value;if(Schemafilename.length>0) {return true; } else { alert("File Name cannot be empty");return false; } }   we written code in button click . i need a file author name . how to find it can any tell me . thanks in advance Hi harish448 For your information, I know you want to get the file's author name, which from client-side But I think Not all type of files have the attribute of "author name" (I think on...

How to upload file without using file control...???
Hi, I have List of file path of Client. say "C:/WordDoc/Document1.doc". Now i want to upload this file to server.I dont want to use File Upload or Input File HTLM Control. Only i have is Path in some lable or text box. Now, How to upload this file from client to server? I dont have idea on it. Java has Third party API for uploading but i dont have idea on it. Can one help me on it. Whether some ActiveX or API avialble to do so. Thank You In Advance...!!!   ---Bhavin I can't see any worthwhile reason to not use the FileUpload control, but..... http://www.aspfree.com/c/a/....

Uploading Files without using File control
Hi Everyone, I'm in need of a feature where i need to upload a file without using File control. Anyone have pointers to go about it ? I've already gone through this tutorial: http://www.15seconds.com/issue/010522.htmbut when i try to run the code it gives me the error on window 2000 PC:"Safety settings on this computer prohibit accessing a data source on another domain."even though security setting set to "Low". And on XP machine i get the error:"Can't create automation server" (or something like that not entirely sure)My actual requirement is to upload a file using AJAX.Help will be gr...

Upload file withouit using the file control
hi is there a way to upload files withouit using the  file control Not that I know of. I would require some kind of component to be installed Client Side (ActiveX etc.). Read this article for more information.RegardsAndre Colbiornsen ---------------------------------Seventh DayRåbygatan 1A,SE-223 61 LundSwedenMob.: +46-(0)708-97 78 79Mail: info@seventhday.se--------------------------------...

How much size of file can be uploaded with file upload control
How much size of file can be uploaded with file upload control? By default its 4mb but it can be chaged through web config settingsAshok Rajawww.iGold.inDon't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved. For a detailed explanation on your query have a look into this article http://msdn2.microsoft.com/en-us/library/e1f13641(vs.71).aspxAshok Rajawww.iGold.inDon't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point...

Upload Image In Gridview using File Upload Control
Hi, I have question that i want to upload image in Gridview for differt users. And i also don't know how to fix size while upload the image.Can You please tell me how to upload images for the different users and how to fix size so i can save all users name with their own logo image. I just have started work with asp.net so i will be very thankful to you if you can give answer with code.       Hi, Nisha Shah Based on my experience, please refer to the following code: //HTML <asp:GridView ID="GridView1" runat="serv...

File Upload: Without using File Input control
Can anybody just tell me how to store a single file from client machine on to server without using a HTML File Input Control. Regards, Kiran You can use the FileUpload control, but that just renders an HTML file input control in the end.  If you don't want to use any HTML stuff, you could use an ActiveX control.  There are plenty available by searching.Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you! I tried to find from your serach string but I could not find the exact code in Asp.Net. Can you Please send me&nbs...

Web resources about - Add link buttons to each file uploaded using File upload control - asp.net.presentation-controls

Help:Files - Wikipedia, the free encyclopedia
... the first step in using an image or other media file is to upload it to Wikimedia Commons , but for legal reasons, some files must be uploaded ...

Instagram Deleting Photos Uploaded Via Third-Party Windows Phone 8 App Instance
... has apparently run afoul of Facebook-owned photo-sharing network Instagram ’s policies, as Developer Daniel Gary told The Verge photos uploaded ...

YouTube Integrates Facebook Connect to Autoshare Uploaded Videos
... rivals on multiple fronts, YouTube announced today that it has just launched Facebook Connect integration, making it easy to autoshare uploaded ...

How many photos are uploaded to Facebook each day?
Answer (1 of 2): We currently have over 200 million photos uploaded per day, or around 6 billion per month. There are currently almost 90 billion ...

Gridplay - The Vine & IG Music Video Maker, Uploaded from Your Camera Roll on the App Store on iTunes ...
Get Gridplay - The Vine & IG Music Video Maker, Uploaded from Your Camera Roll on the App Store. See screenshots and ratings, and read customer ...


Happy Iranians - Original uploaded Iran version of Pharrell Williams " Happy " video - YouTube
Join me at https://www.facebook.com/Sufinumeerpage Young Iranians Arrested For Dancing In 'Happy' Pharrell Video Age: 25, Crime: Happiness About ...

#MyDubai: half a million images uploaded in four months
The number of hits over such a short period of time show how the project has sparked UAE residents’ imaginations, the state news agency Wam reported. ...

Throwback of the Day: Watch the First Video Ever Uploaded to YouTube
This 18-second clip titled “Me at the Zoo” is the very first video ever uploaded to the site 10 years ago today (April 23, 2005). And while it ...

A million minutes of history uploaded online
The AP and British Movietone will upload more than a million minutes of historical footage to YouTube.

Resources last updated: 11/21/2015 1:31:15 AM