简体   繁体   English

如何在HTML img标签中将变量传递给src属性

[英]How to pass a variable to src attribute in HTML img tag

the image in my .aspx file .aspx文件中的图像

 <img src='<%=vPath%>' alt="" id="image" />

in aspx.cs 在aspx.cs中

 public string vPath  ;
 protected void BtnSearchFarms_Click(object sender, EventArgs e)
    {string vImageName = LblFarmId.Text;

string vPath = "~/attachments/survey/" + vImageName + ".jpg";}

how can I pass the vPath variable to src in the img tag? 如何将vPath变量传递给img标签中的src? is it correct? 这是正确的吗? it still doesn't show any result in the img tag when I execute the page 当我执行页面时,它仍然没有在img标签中显示任何结果

Not sure exactly what you are trying to achieve or whether you are going about it correctly, but the very simplest, quick and easy way to do this is: 不确定确切要实现的目标还是正确的实现目标,但是最简单,快速,简便的方法是:

<asp:Button id="BtnSearchFarms" OnClientClick="imageToHiddenField()" />
<input type="hidden" name="imgHidden" id="imgHidden" />

Then javascript: 然后使用javascript:

function imageToHiddenField() {
    document.getElementById("imgHidden").value = document.getElementById("image").src;
}

The value will be added to the hidden field and will go back with the posted data. 该值将被添加到隐藏字段中,并将与发布的数据一起返回。 It can be retrieved using Request.QueryString["imgHidden"] or Request.Form["imgHidden"] depending on whether you are using POST or GET. 可以使用Request.QueryString [“ imgHidden”]或Request.Form [“ imgHidden”]来检索它,具体取决于您使用的是POST还是GET。

Not elegant, but it should work. 不优雅,但应该可以。

It's hard to tell from your question but it sounds like you have an image on your page and you're trying to change the src based on user input? 您的问题很难说出来,但听起来您的页面上有一张图片,并且您正在尝试根据用户输入更改src?

I'd say instead of using <img> the easiest way is to use an <asp:Image> which then has an ImageUrl property you can set. 我要说的不是使用<img> ,最简单的方法是使用<asp:Image> ,然后它具有可以设置的ImageUrl属性。 An ASP Image works generally like a regular img except you can use ~ in your paths. 除了可以在路径中使用〜之外,ASP Image的工作原理通常类似于常规img。

protected void BtnSearchFarms_Click(object sender, EventArgs e)
{
    string vImageName = LblFarmId.Text;
    theAspImage.ImageUrl = "~/attachments/survey/" + vImageName + ".jpg";
    theAspImage.Visible = true;
}

And in the aspx: 在aspx中:

<asp:Image id="theAspImage" runat="server" Visible="false" />

If you can't use an asp:image then you need to resolve the path so it doesn't have a ~. 如果您不能使用asp:image,则需要解析路径,使其不带有〜。 You can use Page.ResolveUrl("~/attachments/survey/" + vImageName + ".jpg") which will convert it to a web accessible path. 您可以使用Page.ResolveUrl("~/attachments/survey/" + vImageName + ".jpg")将其转换为可通过网络访问的路径。

One important note, very important note, you have string vPath inside your event handler. 重要说明之一,非常重要的说明是,事件处理程序中包含string vPath That means you're creating a new variable not modifying your public instance variable. 这意味着您正在创建一个新变量,而不修改您的公共实例变量。 Change: 更改:

string vPath = "~/attachments/survey/" + vImageName + ".jpg"; To: vPath = "~/attachments/survey/" + vImageName + ".jpg"; 要: vPath = "~/attachments/survey/" + vImageName + ".jpg";

If neither of those answer your question, then I think we need more information. 如果这些都不能回答您的问题,那么我认为我们需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM