-1

Actually I am looking to show an image in my div by calling a function of code behind file. Since my image tag is dynamically binding from JavaScript, how can I call a function with parameter. Below is the code which I used to call, but its not working, even its not hitting the function in debug mode.

document.getElementById("divthumbnail").innerHTML = '<img src="'<%=test()%>'"/>';

So, how can I modify this code, to get an image URL?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Muzz
  • 125
  • 1
  • 5
  • 20
  • 1
    As server side code is not direct available client side, you'll need AJAX to do that. – Asons Mar 13 '18 at 13:49
  • The suggested duplicate will suggest web methods etc. However, if this is ASP.NET WebForms why don't you make the image control `runat=server` and set the URL in the page load event? Alternatively you could add a Literal control and write some JS into it from page load - just a `var` containing the url string, perhaps. – Stephen Kennedy Mar 13 '18 at 18:26

1 Answers1

0

You will need a ScriptManager component as required in you markup like this:

<asp:ScriptManager ID="ScriptManager1" runat="server" 
    EnablePageMethods="true">
</asp:ScriptManager>

and having this javascript:

<script type="text/javascript">
    function myFunction() {
        PageMethods.YourMethod();
    }
</script>

Then you can define a WebMethod in your code behind like this:

[System.Web.Services.WebMethod]
public static string YourMethod()
{

}
Eagle
  • 31
  • 5