Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have two Buttons in my ASP.NET page.
<asp:Button ID="UpLoadFile" runat="server" Text="File Selection" CssClass="FileSelection" OnClientClick="fireFileClick()"/>
<asp:FileUpload ID="UpLoadButton" runat="server" cssclass="FakeButton" OnChange="UpLoadBtn"/>

One is a regular Button and the other is a `FileUpload` Button. That is used in order to gain the ability to change the name of the Button I use.
Of course I have and a `script` in order to fire the `click` event in `FileUpload` Button:
function fireFileClick() {
    var objfile = Object;
    objfile = document.getElementById("");
    objfile.click();
}                       }

That script works just fine, triggers the `FileUpload` Button and gives the ability to select my willing file to upLoad.
Now here comes the time in order to act the `FileUpload` Button and that is made by caching the `OnChange` event. And this action is made it perfect.
As you can see in my previous refer I send this event action to a Public Shared procedure:
Public Shared Sub UpLoadBtn(sender As Object, e As EventArgs)

Here an Error is thrown:
Unhandled exception at line 115, column 119 in http://localhost:50761/Pages/Production.aspx
0x800a1391 - JavaScript runtime error: 'UpLoadBtn' is undefined


In this error I've seeing that
/Pages/Production.aspx
some times is
/Account/UsersHandler.aspx


Which is the last page from where I come to the `Production` page.
But that doesn't matter so much, because even if it is the right one the error throws it.

What I have tried:

I search allot around this issue but I haven't found anything related to this.
May be Unlucky OK.
Is there some one to assist me on this?
Posted
Updated 17-Jan-19 12:45pm
v2

1 solution

You can't call your server-side public method in your onchanged event directly as your client(HTML) doesn't know about your public method. One option is to use AJAX with PageMethod to that. Here's an example: https://www.aspsnippets.com/Articles/Calling-ASPNet-AJAX-PageMethods-using-ScriptManager-Example.aspx[^]
 
Share this answer
 
Comments
Lefteris Gkinis 17-Jan-19 20:24pm    
I've made it as you saw me... like that:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div id="FileBrowserDiv" runat="server"  class="FileBrowserDiv" >
<asp:Button ID="UpLoadFile" runat="server" Text="File Selection" CssClass="FileSelection" OnClientClick="fireFileClick()"/>
<script type="text/javascript">
	    function fireFileClick() {
	      var objfile = Object;
	      objfile = document.getElementById("<%= UpLoadButton.ClientID %>");
objfile.click();
}
</script>
<asp:FileUpload ID="UpLoadButton" runat="server" onchange="runasub()" cssclass="FakeButton" />
<script type="text/javascript">
 	function runasub() {
PageMethods.ControlHasFile.document.getElementById("<%=UpLoadButton.ClientID%>").value;
}
</script> 


But this throws me an error:
Unhandled exception at line 159, column 9 in http://localhost:50761/Account/UsersHandler.aspx
0x800a1391 - JavaScript runtime error: 'PageMethods' is undefined


The `ControlHasFile` is the `Public Shared Sub` which is in code behind.
And with this Sub I want to handle the File that I select.
Vincent Maverick Durano 22-Jan-19 1:03am    
You need to decorate your server method with the WebMethod attribute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900