设置使用ASP.NET功能IMAGEURL [英] Setting imageURL using a function in ASP.NET

查看:546
本文介绍了设置使用ASP.NET功能IMAGEURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做过这个任务,中继器内,它一直。不过,我不能让下面为我在一个正常的web表单页面工作。图像显示为断开的链接和断点我放在codebehind不会被触发。

I've done this task before within repeaters and it has worked. However I can't get the below to work for me in a normal webforms page. The images appear as broken links and breakpoints I put in the codebehind are not triggered.

(在aspx文件)

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImageDirectory()%>btnRunReport.png'  />

(codebehind)

(codebehind)

public string GetImageDirectory()
{
    return "~/App_Variants/LBSX/images/";
}

这是我尝试过,在另外一个我试图通过传递imagename作为字符串第二种方法,它会返回整个链路的方式。仍然没有运气!

This is the second method I've tried, in the other one I tried passing the imagename through as a string, and it would return the entire link that way. Still no luck!

有什么想法?

谢谢!

感谢大家的帮助。在得心应手的提示结束后,我发现其中的伎俩递归片断如下:

Thanks for the help everyone. In the end after the handy hints I found a recursive snippet which did the trick as follows:

private void UpdateImages(Control Parent)
{
    foreach (Control c in Parent.Controls)
    {
        ImageButton i = c as ImageButton;
        if (i != null)
        {
            i.ImageUrl = "~/App_Variants/LBSX/images/" + i.ImageUrl;
        }
        if (c.HasControls())
        {
            UpdateImages(c);
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    UpdateImages(Page);
    ...

希望它可以帮助别人。

Hope it helps someone else.

干杯

推荐答案

首先,就像扎卡里提到的,你使用数据绑定code座。

First, just like Zachary mentioned, you're using the code block for data binding.

二,因为你已经尝试过,使用内联前pression(&LT;%=%&GT; )将不会在你的情况下工作,要么,因为你不能使用内联前pression一个服务器标签的任何属性。

Second, as you've already tried, using an inline expression (<%= %>) won't work either in your case, since you can't use an inline expression for any property of a server-tag.

你可以做什么,而不是使用HTML语法定义图像按钮,省略了 =服务器标记,并使用内嵌的前pression得到图片的网址:

What you could do instead is defining an image button using HTML syntax, omitting the runat="server" tag, and use the inline expression to get your image's URL:

<input type="image" src="<%= GetImageDirectory() %>btnRunReport.png" name="image" />

一个内联前pression做什么,它调用的Response.Write()与之间的&LT的价值;%=%&GT ; 作为参数,例如: &LT;%= this.MyVar%GT; 的Response.Write(this.MyVar)

What an inline expression does is, it calls Response.Write() with the value between <%= %> as the parameter, e.g. <%= this.MyVar %> is Response.Write(this.MyVar).

这篇关于设置使用ASP.NET功能IMAGEURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆