简体   繁体   English

如何在C#中的代码中使用ImageButton

[英]How to work with ImageButton in code behind in C#

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string path = Server.MapPath("~//Images//" + FileName);
            FileUpload1.SaveAs(path);
            string imagepathsource = path;
            string imagepathdest = @"D:\\" + Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName;
            File.Move(imagepathsource, imagepathdest);

            uploadedimage.ImageUrl = "D://" + Session["brandname"].ToString() + "//" + Seasonfolders.SelectedItem.Text + "//" + stylefolders.SelectedItem.Text + "//Images//" + FileName;
        }
    }

uploadedimage is a ImageButton where I need to display the image using imageurl with that link. uploadedimage是一个ImageButton,我需要使用带有该链接的imageurl来显示图像。 The image is not displaying and no error.. I could see when the above code gets execute the page is getting refreshed? 图像没有显示,没有错误..我可以看到上面的代码执行时页面正在刷新? What code can be added in this ?? 可以添加什么代码?

ImageUrl needs an URL. ImageUrl需要一个URL。

So instead of assigning it with a local file name on disk, use something like this (where ~ stands for 'application root folder': 因此,不要在磁盘上使用本地文件名分配它,而是使用类似这样的东西(其中~代表'应用程序根文件夹':

uploadedimage.ImageUrl = "~/Images/yourImage.png"

You have to supply it with: 你必须提供:

  • An image inside your application root folder; 应用程序根文件夹中的图像;
  • An URL that is catched by a HttpHandler, that generates / loads the image from a different location (a little harder to do, but if you need to load from another location then inside application root, this is the best option). 由HttpHandler捕获的URL,它从不同的位置生成/加载图像(稍微难做,但如果您需要从另一个位置加载,那么在应用程序根目录内,这是最佳选择)。

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

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