如何从asp.net动态调用img src值

时间:2016-11-05 16:48:00

标签: asp.net html5 webforms

我想使用asp.net和C#代码传递src值。

其实我想使用多个HTML标签,我想在其中设置来自asp.net和C#代码的值

C#WebForm代码背后:

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="[passed_value]" width="100" height="100" />
<video controls="controls" poster="[passed_value]" width="550" height="320">
   <source src="../videos/2.mp4" type="video/mp4"> 
</video> 

我希望使用asp.net webforms

传递动态srcposter

请帮我解决这个问题......

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用Response.Write shorthand syntax <%=variable%>来编写变量值,如下所示:

C#WebForm代码背后:

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> 

注意: 上述问题评论中Dev Sharma's解决方案为后代发布的答案。