Hi rdelgadoj,
Sorry for my late reply ....
I think that the value of the input control (type=file) is not remembered during postback.
One thing is weird: If I put runat="server" on my File1 control, it still gets triggered and the Open File Dialog still pop's up? (But, it doesn't have to be runat="server" since the value isn't remembered during postback)
You can solve this by doing the following ....
-
in the input control (type=file) you have an event onchange.
-
This event gets triggered whenever the value of the control changes
-
When that occurs, call a javascript function to get the value of the input-control (type=file)
-
Store that value in an input-control (type=hidden)
-
make sure the input control (type=hidden) is runat="server".
-
then in code-behind, get the value out of the hidden input control. (This value is remembered during postback!)
Here is an example:
html:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function triggerFileUpload()
{
document.getElementById("File1").click();
}
function setHiddenValue()
{
document.getElementById("Hidden1").value = document.getElementById("File1").value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input runat="server" id="Hidden1" type="hidden" />
<input runat="server" id="File1" type="file" onchange="setHiddenValue()" style=" visibility:hidden;" />
<br />
<br />
<asp:Button ID="Button1" OnClientClick="triggerFileUpload()" runat="server" Text="ASPNET Button" />
<br />
<br />
<input id="Button2" type="button" onclick="triggerFileUpload()" value="HTML Button" />
<br />
<br />
<asp:Button ID="Button3" runat="server" Text="Go CodeBehind To Get Input Value" />
</div>
</form>
</body>
</html>
code-behind:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim inputValue As String
inputValue = Me.Hidden1.Value
End Sub
End Class
Hope this helps!
Wim
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.