0

I have an asp.net page with a FileUpload control so that the user can browse to select a file. I wanted to assign a default value so the user doesn't need to browse unless they're doing something out of the norm, but the File properties are read-only.

So I set the Visible property of the FileUpload control to false and put in a textbox and button. In that textbox I put the default path I want. When the user clicks the button I want it to open the Browse window that clicking the FileUpload control would. I have this:

<head>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE"/>
<meta content="JavaScript" name="vs_defaultClientScript"/>
<meta content="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema"/>   

<script src="common.js" type="text/javascript"></script>

    <script type="text/javascript">
        function fileBrowse()
        {
            document.getElementById('FileBrowse').click();
        }
    </script>

</head>

And then in the code I have this:

Protected Overrides Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    MyBase.Page_Load(sender, e)

    Me.btnINI.Attributes.Add("onclick", "javascript:fileBrowse();")
End Sub

FileBrowse is the name of the FileUpload control on the page. btnINI is the name of the button that I want to launch the browse.

When I click the button nothing happens - no errors, nothing is launched. Any help would be much appreciated!

Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
MF Luder
  • 371
  • 1
  • 8
  • 21
  • See http://stackoverflow.com/questions/1133058/emulate-a-file-upload-click-in-jquery – Eli Gassert Jan 18 '13 at 16:52
  • You are showing us *server side code* but describing a *client side problem*. What HTML does the ASP generate? Is your problem that the HTML you want doesn't do what you think it should, or is it that you can't generate the HTML you want from the ASP? – Quentin Jan 18 '13 at 16:52
  • It would appear that the HTML is correct. When I load the page it shows: – MF Luder Jan 18 '13 at 17:35

2 Answers2

1

My guess is the id is not correct. Try passing the ClientID of FileBrowser control to the javascript function as a parameter. Also, check if the function is actually called with an alert box.

fscan
  • 433
  • 2
  • 9
  • I modified the function: function fileBrowse(ID) { document.getElementById(ID).click() return false; } And then the code: Me.btnINI.Attributes.Add("onclick", "javascript:fileBrowse('FileBrowse');") I get the same results. – MF Luder Jan 18 '13 at 17:35
  • Me.btnINI.Attributes.Add("onclick", "javascript:fileBrowse('" & FileBrowse.ClientID & "');") – fscan Jan 18 '13 at 17:39
  • Worked perfectly! Thank you so much! – MF Luder Jan 18 '13 at 17:55
0

I'm pretty sure you can't set any values in the file selection dialog via javascript. There are serious security issues with doing that and the browsers prevent it.

NotMe
  • 87,343
  • 27
  • 171
  • 245