Dev Sharma Dev Sharma - 9 months ago 58
Javascript Question

How to paas img src value dynamically from asp.net

i want to pass src value using asp.net and C# code.

Actually i want to use multiple HTML tags in which i want to set values from asp.net an C# code

<img src="[passed_value]" width="100" height="100" />


or

<video controls="controls" poster="[passed_value]" width="550" height="320">
<source src="../videos/2.mp4" type="video/mp4">
</video>


I want pass dynamic
src
and
poster
value using asp.net webforms

Please help me regarding this...

Answer Source

The simplest solution is to write the variable values using the Response.Write shorthand syntax <%=variable%> like so:

C# WebForm Code Behind:

public string path; 
public string posterPath; 

protected void Page_Load(object sender, EventArgs e) { 
    path = "../videos/2.mp4"; 
    posterPath = "../images/user.jpg"; 
} 

HTML:

<img src="<%=path%>" width="100" height="100" />
<video controls="controls" poster="<%=posterPath%>" width="550" height="320">
   <source src="../videos/2.mp4" type="video/mp4"> 
</video> 

Note: Answer posted for posterity from Dev Sharma's solution in question comments above.