-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?

2
  • 1
    As server side code is not direct available client side, you'll need AJAX to do that. Commented Mar 13, 2018 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. Commented Mar 13, 2018 at 18:26

1 Answer 1

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()
{

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.