简体   繁体   中英

how to set image URL as absolute physical path?

i have image-button control in a grid view my customer need to enter a path of folder in another page i stored this path in database i need because customer store images in external hard cannot transfer image from hard disk to website images because it has a huge size

my image table contain image-name when user display grid-view i concatenate image-name from database and path from database also how can i do that

            <asp:TemplateField HeaderText="photo">
           <ItemTemplate >


               <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImagePath()+Eval("ImageName")  %>'  Width="100px" Height="100px" Style="cursor: pointer" OnClientClick = "return LoadDiv(this.src);" /> 
           </ItemTemplate>
           </asp:TemplateField>

you need write down in you function like this

public string GetImage(string name)
{
  string path = Path.Combine(Server.MapPath(("~/Admin/Images/" , name)); 
  return path;
}

or

you can also do like this

void GrdVw_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        Image rowImage = (Image) e.Row.FindControl("currentDocFile");
        rowImage.ImageUrl = whatever;
    }
}

I suggest pass the image name as parameter to GetImagePath Try this:

public string GetImagePath(string imageName)
{
   return VirtualPathUtility.ToAbsolute("~/images/" + imageName);
}

VirtualPathUtility.ToAbsolute(string) converts a virtual path to an application absolute path. So you will get something like "/images/yourimage.jpg".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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