Firefox and the FileUpload control in ASP.NET

Jan 17, 2008

I’ve been pretty annoyed about the fact that FireFox ignores the width property of the ASP.NET FileUpload control. It doesn’t matter if you set it by CSS or even in JavaScript, Firefox always ignores it and that results in a crippled layout since you have no control over the width of the upload input field. This actually has nothing to do with the FileUpload control, but with the rendered output of that control, which is just a standard <input type="file"...> field.

Here the width is set to 400px. The FileUpload control renders the Width attribute to the correct CSS style value of 400px at runtime.


<asp:FileUpload runat="server" ID="txtUpload" Width="400" />

But as mentioned, Firefox totally ignores that. Then I remembered a forum thread I read some years ago about a size attribute. The only thing I could find was this list of available HTML attributes, but I gave it a try.

This is what I ended up with. The size attribute actually works in Firefox and IE ignores it, but adheres to the Width attribute instead.


<asp:FileUpload runat="server" ID="txtUpload" Width="400" size="50" />

The size attribute determines the number of characters should be visible and then adjusts the width accordingly. It is not the same as setting a maximum length of the file name, so it doesn’t break anything.

You should think that since a size of 50 generates the same width as 400px that you could always just divide the width in pixels with 8 to find the equivalent size. That is not the case. You have to manually adjust every time until you find the right size value that matches the pixel width.

Don’t you just love these kinds of obscure browser differences?

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (10) -

Miron
Miron Israel
1/17/2008 8:41:16 PM #

Thanks Mads, really useful tip.

Filip
Filip Australia
1/17/2008 11:21:38 PM #

Mads,
Not sure if this helps - haven't tried, but instead of just Width="400" try to put in Width="400px". I remember I had to do something similar, not for FileUpload though, to make it recognise the size in FireFox.

HTH

Cameron
Cameron United States
1/18/2008 12:06:33 AM #

Thanks Mads, I just added a FileUpload control about two hours ago but didn't spend any time getting it formatted yet, this will save me some looking around time. Smile

David
David
1/18/2008 4:31:48 AM #

Why not use css to determine the width?
#txtUpload { width;400px; }

Mads Kristensen
Mads Kristensen Denmark
1/18/2008 6:38:34 AM #

@Filip and @David,

When you set the Width="400" on the FileUpload or any other web control in ASP.NET 2.0, it will render as style="width:400px". This means that it uses standard CSS to set the width and that is still ignored by Firefox.

Michel
Michel France
1/18/2008 10:43:29 AM #

There is a post on Quirksmode which explain how to style an input type="file" : http://www.quirksmode.org/dom/inputfile.html

Josh Stodola
Josh Stodola United States
1/18/2008 12:51:58 PM #

Yes, I hate these kind of differences, but I would not blame Firefox for this.  IE is the problem for not recognizing the size attribute.  File input controls have legendarily been difficult (nearly impossible) to style, and it comes as no surprise to me that Firefox ignores most CSS attributes.  ASP.NET really should render the FileUpload control to something that would work in both browsers (both the size attribute and the accompanying CSS).  Have a look at what the W3C has to say about the size attribute of input elements...

www.w3.org/TR/html4/interact/forms.html#h-17.4

In a perfect world, ASP.NET would properly interpret the Width value and convert it to Size, and IE would recognize it.

Jeff
Jeff United States
1/18/2008 9:59:06 PM #

I found that if you adjust for Firefox the page does'nt load well in IE or vice versa. Then you have to consider they are not the only browser. What a mess. But I experimented and came up with a code that works in all browsers I am aware of.
<table border=0 cellpadding=2 cellspacing=0 width="100%" height="100%"> You may have to use it to start every table on a page but it works.
If anybody has a problem or has something better, let me know.

Joshua Folkerts
Joshua Folkerts United States
1/23/2008 10:01:19 PM #

This saved me a headache and a half.  Thanks brutha

sumi
sumi United States
3/4/2009 9:16:38 AM #

Thanx Mad, it is really helpfull for me

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.