473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Get FileUpload control value

Get FileUpload control value

Hi guys, I have an aspx webpage with 9 fileupload controls called (fupBlogImg1,fupBlogImg2,.........fupBlogImg9 ). I want to get their values with a for at the code after the button (cmdAlbumCreate) is clicked.

Here is the code but fupBlogImg is null (I dont know why)
for (int n = 1; n < 10; n++)
{
string StrBlogImg = "fupBlogImg" + n;
FileUpload fupBlogImg = (FileUpload)FindControl(StrBlogImg);

Thank you all
Jan 21 '08 #1
7 4025
Frinavale
9,735 Expert Mod 8TB
Get FileUpload control value

Hi guys, I have an aspx webpage with 9 fileupload controls called (fupBlogImg1,fupBlogImg2,.........fupBlogImg9 ). I want to get their values with a for at the code after the button (cmdAlbumCreate) is clicked.

Here is the code but fupBlogImg is null (I dont know why)
for (int n = 1; n < 10; n++)
{
string StrBlogImg = "fupBlogImg" + n;
FileUpload fupBlogImg = (FileUpload)FindControl(StrBlogImg);

Thank you all
Instead of declaring 9 individual file upload controls, consider using an array of 9 file upload controls. Then you will be able to loop through the array as you desire.

-Frinny
Jan 21 '08 #2
Instead of declaring 9 individual file upload controls, consider using an array of 9 file upload controls. Then you will be able to loop through the array as you desire.

-Frinny
Thanks Frinny but how would I do that? please
Jan 21 '08 #3
protected void cmdAlbumCreate_Click(object sender, EventArgs e)
{
FileUpload fupBlogImg1 = (FileUpload)FindControl("fupBlogImg1");........

The code above says fupBlogImg1 is null but I have that control created on the page?? any idea?

Ty all
Jan 21 '08 #4
Frinavale
9,735 Expert Mod 8TB
protected void cmdAlbumCreate_Click(object sender, EventArgs e)
{
FileUpload fupBlogImg1 = (FileUpload)FindControl("fupBlogImg1");........

The code above says fupBlogImg1 is null but I have that control created on the page?? any idea?

Ty all
Could you please post more of your code so that I can get a better idea of what you mean when you say you've create the control on the page.

In the mean time check out this article (it's still in a rough draft and the example's in vb. But you should be able to get the idea of how to dynamically create controls through it)

-Frinny
Jan 21 '08 #5
HERE IS THE CODE:

PAGE.ASPX
Expand|Select|Wrap|Line Numbers
  1. <asp:FileUpload ID="fupBlogImg1" runat="server" Width="298px" />
  2. <asp:FileUpload ID="fupBlogImg2" runat="server" Width="298px" />
  3. ............
  4. <asp:FileUpload ID="fupBlogImg9" runat="server" Width="298px" />
  5. <asp:Button ID="cmdAlbumCreate" runat="server" Text="Inserir imagens no álbum" OnClick="cmdAlbumCreate_Click" /> 
  6.  
PAGE.ASPX.CS
Expand|Select|Wrap|Line Numbers
  1. protected void cmdAlbumCreate_Click(object sender, EventArgs e)
  2. {
  3. FileUpload fupBlogImg1 = (FileUpload)FindControl("fupBlogImg1");
  4. FileUpload fupBlogImg2 = (FileUpload)FindControl("fupBlogImg2");
  5. .............
  6. FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");
  7.  
but fupBlogImg1,2...9 is returning null. I have tryied Page.FindControl but didnt work

If you need anything else to get a better understanding pls let me know.

TY
Jan 21 '08 #6
Frinavale
9,735 Expert Mod 8TB
HERE IS THE CODE:

PAGE.ASPX
<asp:FileUpload ID="fupBlogImg1" runat="server" Width="298px" />
<asp:FileUpload ID="fupBlogImg2" runat="server" Width="298px" />
............
<asp:FileUpload ID="fupBlogImg9" runat="server" Width="298px" />
<asp:Button ID="cmdAlbumCreate" runat="server" Text="Inserir imagens no álbum" OnClick="cmdAlbumCreate_Click" />
PAGE.ASPX.CS
protected void cmdAlbumCreate_Click(object sender, EventArgs e)
{
FileUpload fupBlogImg1 = (FileUpload)FindControl("fupBlogImg1");
FileUpload fupBlogImg2 = (FileUpload)FindControl("fupBlogImg2");
.............
FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");

but fupBlogImg1,2...9 is returning null. I have tryied Page.FindControl but didnt work

If you need anything else to get a better understanding pls let me know.

TY
You don't need to Redeclare the FileUplaod controls.
You have declared them in your aspx code and these variables can be accessed in your C# code.

For instance if you want to retrieve the file name of the file being uploaded in the fupBlogImg9 you would do something like:

String fileName = fupBlogImg9.FileName;

You don't need to do this:
FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");
You already have a fupBlogImg9 FileUpload control to use.

I think you're losing the object state for your FileUpload control when you are redeclaring it.
Jan 21 '08 #7
Plater
7,872 Expert 4TB
I rather think that doing this:
Expand|Select|Wrap|Line Numbers
  1. FileUpload fupBlogImg9 = (FileUpload)FindControl("fupBlogImg9");
  2.  
Is what is causing the problem, you're trying to both reference and re-declare a variable with the same name from two different scopes.

If you did not want an array, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. FileUpload tempfup =null;
  2. for(int i=0;i<10;i++)
  3. {
  4.    Control c = FindControl("fupBlogImg"+i.ToString());
  5.    if(c!=null)
  6.    {//a control was found
  7.       if(c.GetType()==typeof(FileUpload))
  8.       {//there are better ways, but I always like to check to make sure
  9.          tempfup = (FileUpload)c;
  10.          //now do something with the values of the tempfup object
  11.       }
  12.    }
  13. }
  14.  
Jan 21 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Harinath | last post by:
Hi All, I am working on a portal using .net2.0. In one of my page, i have FileUpload control. I am doing some server side validation for form fields in general. if there is any validation...
3
by: rn5a | last post by:
The ASPX FileUpload control displays a TextBox along with a 'Browse...' Button. Setting the different properties of this control just reflects the changes in the TextBox but not the Button. For...
1
by: Nathan Sokalski | last post by:
I have an ASP.NET 2.0 webform which includes both a FileUpload contol and a Calendar control. When the user selects a new data with the Calendar control, the FileUpload control is reset (there is...
8
by: bsm | last post by:
Hi, I want to Set asp:fileupload Filename at runtime. When I try to do, it shows "Readonly" error. Anyway to achieve this.
2
by: Bart | last post by:
Hi, i use the FileUpload control but if the uploaded file already exists, i want let the choice between overwriting or not the existing file. Therefore i use a radiobuttonlist with 'yes' and...
5
by: =?Utf-8?B?QmVydA==?= | last post by:
Hi How can you set the filename property in the asp:fileupload control? How can you persist the value of the chosen filename between postbacks? thanks B
5
by: =?Utf-8?B?VEQgaXMgUFNQ?= | last post by:
I have a FileUpload control within a FormView (although I tried it outside the FormView and got the same result) in the InsertTemplate. When the <Insertbutton is clicked and the Insert Command is...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a gridview with 2 columns. One column is a BoundColumn to a part number (string). One column is an ItemTemplate with a FileUpload control. There can be multiple rows (i.e. part numbers)...
1
by: bharathreddy | last post by:
Hi, In my page I have an updatepanel and in the updatepanel I have a wizard in the wizardstep2 i have a user control in the user control i have the fileupload. I have added the triggers to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.