0

I've got a HTML input in my asp.net page:

<input type="file" name="image" accept="image/*" id="AttachMe">

This is all fine and dandy - it opens the file browse dialog etc.

However, what I would like to do is:

1 - make this invisible (if at all possible - if not I can just move it elsewhere in the page)

2 - trigger this from codebehind

I've got multiple rows in a datalist, each with a button on. The button loads up the relevant bit in C# which finds out which row we've pressed the button on. I then want it to load up the HTML file input.

Unfortunately, I can't simply put the HTML input in each row as we're talking about mobile handsets and space is limited (I tried). Plus I'd like to get the URL of the selected file (I think that's pretty easy to get anyway).

But mainly, I need to know just how to trigger the HTML file input from C# - so I can basically click the button and C# will then trigger the HTML file input for me.

EDIT - this is the code I now have:

My FileUpload:

<asp:FileUpload id="FileUploadControl" runat="server" />

The script to call it:

<script type="text/javascript">
        function AttachAFile() {
            document.getElementById('<%= FileUploadControl.ClientID %>').click();
        }
    </script>

This script is called from within the C# codebehind. It is called (I've tried it with alerts and calling other buttons).

However, it's still not calling the FileUploadControl for some reason. Anyone any ideas? I've come across similar topics which all seem to have worked out doing exactly the same as I'm doing (and the FileUploadControl does work on its own).

user25730
  • 517
  • 2
  • 6
  • 24
  • 1
    It has an ID. So use that ID to associate a CSS class, one which is similar to `display:none;` You may need to run it at server and use asp: controls. Or, just surround the thing with an if statement – MyDaftQuestions Nov 05 '15 at 14:35
  • Unfortunately, I've tried searching and haven't found anything that works (though it could well be I'm looking for the wrong things). I'd love a code example just to get me moving. – user25730 Nov 05 '15 at 14:39
  • I think you will need to activate the file upload with javascript upon clicking on the buttton. Tou can achiev this on loading the page. Something like this. $('input[type=button]').click(function() { $(this).parent(".item") .find('input[type=file]') .trigger('click'); }); – Steven Nov 05 '15 at 15:01
  • Why are you wanting to do this with the server side at all? – MyDaftQuestions Nov 05 '15 at 15:09
  • I don't want this to be done server side. If I'm doing something wrong then please let me know. – user25730 Nov 05 '15 at 15:19
  • I've currently used some code posted by MyDaftQuestions earlier to make the control invisible. Just having some issues getting it to run. I've created a Javascript function that uses `document.getElementById("FileUploadControl").click();` which doesn't seem to be calling it (the function's getting called, it's just not getting to the button). – user25730 Nov 05 '15 at 15:33
  • Possible duplicate of [Programmatically trigger "select file" dialog box](http://stackoverflow.com/questions/8595389/programmatically-trigger-select-file-dialog-box) – Asons Nov 06 '15 at 15:34

0 Answers0