HTML Code ClinicDo you have some HTML code you'd like to share and get suggestions from others for tweaking or improving it? This discussion is the place.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the HTML Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I am creating a HTMLInputFile control in ASP.Net to upload a file. But I am not able to change the appearance of the browse button using style sheets. Is it possible to change the background color of Browse Button in HTMLInputFile control.
I used the following statement and it changes the appearance of the HTMLInputFile control's text box field but not the Browse button that accompanies it.
You ran into a little problem in the <input type="file"> control. Indeed you can't change the button; all you can change is the text box. However, you can cheat around it a little bit. Here's what to do:
1. Add an input type="file" to your page
2. Make it hidden by setting its display style-attribute to none
3. Add a text box to your page that will replace the text part of the file control (it's just used as a fake text box)
4. Add a normal button to your page. Set its style to whatever you want, using the style or class attributes.
5. In the onclick of the second button, fire a JavaScript function that fires the onclick of the HTML file control
6. Once the user has chosen a file, copy the contents of the hidden file control to the second and visible text box, so the user knows what file has been selected.
I also added a readonly="true" attribute to the second text box. This is to prevent users from directly changing the contents of the fake file text box. If you allow them to change it directly, your real, and hidden, file text box will get out of synch.
It's quite a bit of work, it's not completely backwards compatible to ancient browsers, but I'm sure it will do the trick in most circumstances.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
One addition to my previous post: I really believed this example was pretty much cross browser. However, after a bit of testing, it turns out that it only works in Internet Explorer, but not in Netscape, Mozilla, Opera etc.
Not sure why yet, but if I ever figure out the reason, I'll post an update here.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Using code below + an ASP button on ASP.NET, when the ASP-button clicked, the value of htmlinputfile = blank.
Quote:
quote:Originally posted by Imar
Hi Geeta,
You ran into a little problem in the <input type="file"> control. Indeed you can't change the button; all you can change is the text box. However, you can cheat around it a little bit. Here's what to do:
1. Add an input type="file" to your page
2. Make it hidden by setting its display style-attribute to none
3. Add a text box to your page that will replace the text part of the file control (it's just used as a fake text box)
4. Add a normal button to your page. Set its style to whatever you want, using the style or class attributes.
5. In the onclick of the second button, fire a JavaScript function that fires the onclick of the HTML file control
6. Once the user has chosen a file, copy the contents of the hidden file control to the second and visible text box, so the user knows what file has been selected.
I am also having problems with the suggested solution. When I click on the "normal" button, the file browse dialog does indeed appear. However, when I select a file it does not get returned, i.e. document.frmUpload.myFile.value is blank! It works fine if I click on the original browse button of the HTMLInputFile control.
What am I doing wrong? I am using v1.1 of the .NET framework and IE6
I think the problem may be due to the fact that I am using "runat=server" with the HTMLInputFile control, but I need to do this for a reason. Any help would be much appreciated
I think I know what's causing the problem, but don't have a solution:
The original code I wrote uses the OnPropertyChanged()event of the HTMLInputFile control to respond to the user selecting a file, and this works fine. However, the main code for handling the event is server-side, and that's where the problem is. On the server I register a javascript function using RegisterClientScriptBlock, which I call from the clent-side event handler. This allows me to write code on the server to handle this by trapping the postback event.
The problem is: the RegisterClientScriptBlock line in the server-side code seems to prevent the HTMLInputFile from having it's value set correctly after a file has been selected. If I comment this line out it works fine otherwise the control does not contain a value after file selection, and therefore the OnPropertyChanged()event never gets called. However, due to the nature of my application I need to use server-side code to deal with the event.
Any ideas? I know this scenario is quite complicated, but if you have come across a similar problem before please let me know.
..I forgot to mention: When I use the HTMLInputFile control normally everything works fine. There seems to be an conflict in using RegisterClientScriptBlock with the control AND calling the onclick() event dynamically (to simulate the user clicking on the control)