Hello All,
I am presently working on a developed website and want to add some functionality to it.

I have a file called AssetPreview.ascx and the code behind the file as

Now,

i added something to the ascx file like,

<a id="test" onclick = "temp()" runat="server" >Click here to download the file</a>

and so i added the method temp in the as,

public void temp(object sender, EventArgs e)
{
//some functionality
}

Is this is correct way to do this..because when i click that link i am getting an run time error saying

Microsoft JScript runtime error : Object Expected

should i do anything else ???

Can any 1 please help me...
Edit/Delete Message

Dani AI

Generated

Short version for : the runtime error means the browser tried to run a JavaScript function called temp but none existed. An HTML onclick attribute is client-side. To run server code you must wire a server event (or call JS that triggers a postback).

As suggested, use a server event instead of the client onclick. As noted, if you get "No overload for method 'temp' takes '0' arguments' check two things: the markup must reference the handler by name (no parentheses) and the control must be a server control (has runat="server"). Also make sure the code-behind class named in the .ascx Inherits directive matches the file and the handler has the EventHandler signature and appropriate accessibility.

If the goal is a server-side download, a common, simple approach is to use an ASP.NET server control that posts back and then stream the file from the server. Example pattern (use a server control mapping to a handler method that calls the response API):

<asp:LinkButton ID="lnkDownload" runat="server" OnClick="DownloadFile">Download</asp:LinkButton>

The handler should use the response APIs to send the file (see HttpResponse.TransmitFile) so the browser downloads it. For more on server-side anchor events see HtmlAnchor.ServerClick and for file streaming see HttpResponse.TransmitFile: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.transmitfile?view=netframework-4.8 and https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.linkbutton?view=netframework-4.8.

Recommended Answers

All 2 Replies

Change onclick to onserverclick and it will work as you intend.

If u use onserverclick then u will get this error
"No overload for method 'temp' takes '0' arguments"

Instead of that u can go for server control with onclick.

<asp:Button ID="btnClick" Text="Click" runat="server" 
            onclick="btnClick_Click" />

Codebehind

protected void btnClick_Click(object sender, EventArgs e)
    {

    }

OR

If u want html control then u need to go for Javascript function like this,

<input type="button" name="btn" value="Click" onclick="onclickEvent();" />

Invoke javascript function,

]<script language="javascript" type="text/javascript">
        function onclickEvent() {
            alert("Hai");
        }
    </script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.