So I am in a confusing situation right now, I have a ListView with an ItemTemplate that contains an Image element:
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" class="main" runat="server" ImageUrl='<%# Eval("photo1") %>' />
</ItemTemplate>
</asp:ListView>
In my click event I have the following code:
protected void btn_search_Click(object sender, EventArgs e)
{
var img = ListView1.Items[0].FindControl("Image1") as Image;
var lbl = ListView1.Items[0].FindControl("lbl_ID") as Label;
string image = img.ImageUrl;
foreach (ListViewItem item in ListView1.Items)
{
if (img.ImageUrl == "defaultcar.jpg")
{
img.ImageUrl = "images/defaultcar.jpg";
}
else
{
img.ImageUrl = "images/" + "44/" + image;
}
}
}
So in my DB I have 2 images, one called "defaultcar.jpg" and another called "car.jpg" what I am trying to do is create the correct path for each image, I have debugged everything and it seems the first ItemTemplate is displaying "images/44/defaultcar.jpg" and the second ItemTemplate is displaying "car.jpg", it should be the other way round, "images/44/car.jpg" and "images/defaultcar.jpg"
does anyone know what I am doing wrong?
If I understand your issue, you can solve it as below:
First create method to return Image url like:
protected string GetImageUrl(object imageUrl)
{
if(imageUrl == null)
return "";
if(imageUrl.ToString() == "defaultcar.jpg")
return = "images/defaultcar.jpg";
else
return "images/44/" + imageUrl.ToString();
}
Then update .aspx file:
<asp:Image ID="Image1" class="main" runat="server" ImageUrl='<%# GetImageUrl(Eval("photo1")) %>' />
Related
I need to add an image icon to gridview databound at runtime in a specific cell.
I started code already but i'm not sure how to accomplish it because using this code the image don't showing in gridview...
See below:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
ImageButton image = new ImageButton();
image.ImageUrl = "C:/inetpub/wwwroot/img/add-page-red.gif";
e.Row.Cells[2].Controls.Add(image);
}
}
Any help would greatly appreciate... Thank you.
// In .aspx page use below code-
//for imageUrl you have to call the method which is defined in code behind-
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl='<%# GetImage() %>'/>
</ItemTemplate>
</asp:TemplateField>
// In code behind return the image path-
public static string GetImage()
{
return "../Images/Image.jpg";
}
I am trying to display a list of images and on clicking any image, it will get replaced by an "X" image.
So far, I have been able to display the list, but I am not able to make them clickable.
C# code
protected void Page_Load(object sender, EventArgs e)
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Cards"));
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Cards/{0}", System.IO.Path.GetFileName(item)));
}
RepeaterImages.DataSource = images;
RepeaterImages.DataBind();
}
ASPX Content
<asp:Repeater ID="RepeaterImages" runat="server">
<ItemTemplate>
<asp:Image ID="currentImage" runat="server" ImageUrl='<%# Container.DataItem %>' onclick='changeImage()'/>
</ItemTemplate>
</asp:Repeater>
i want to get image url on clicking the image from database in repeater
my database contains(id,url)
my repeater code is:
<asp:Repeater runat="server" ID="repeater" >
<ItemTemplate >
<asp:ImageButton runat="server" Width="200px" Height="200px" ImageUrl='<%#Eval("url") %>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%#Eval("url") %>' />
</ItemTemplate>
</asp:Repeater>
my .cs code is
protected void Image_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string a=e.CommandArgument.tostring();
responce.write(a);
}
}
you can do in ImageClick
((ImageButton)sender).ImageUrl
to get the url of the clicked button
Cast sender to ImageButton and read its ImageUrl property.
Also, is there a reason you are using command instead of handling ImageButtons click event?
Both Vladimir and Guigui answers are prefer way of accessing URL of an image button.
If you also want ID value in addition to URL, you can store multiple values into CommandArgument separated by comma.
<asp:ImageButton runat="server"
ImageUrl='<%#Eval("url") %>'
OnCommand="Image_Command"
CommandName="ImageClick"
CommandArgument='<%# Eval("Id")%> + "," + Eval("url") %>' />
protected void Image_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string[] commandArgs = e.CommandArgument.ToString()
.Split(',');
string id = commandArgs[0];
string url = commandArgs[1];
}
}
I have a grid view which I populated with images from a folder. I am trying to delete the image by getting their path name but it always returns me null:
Here are my codes to populate the grid view with images:
protected void GetImage()
{
string path = HttpContext.Current.Request.PhysicalApplicationPath + #"Story/Food Fit For A King";
string[] files = System.IO.Directory.GetFiles(path, "*.jpg");
IList<ImageFileInfo> imageFileList = new List<ImageFileInfo>();
foreach (string strFileName in files)
{
// Change the Absolute path to relative path of File Name and add to the List
imageFileList.Add(new ImageFileInfo { FileName = ResolveUrl(strFileName.Replace(Server.MapPath("/"), "~/")) });
}
gvStory.DataSource = imageFileList;
gvStory.DataBind();
}
And below is my code to delete :
protected void gvStory_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gvQuestion = (GridView)sender;
int row = e.RowIndex;
// Extract Values.
// string imageName = (TextBox)gvStory.Rows[row].Cells[0].FindControl("TextBox1");// RETURNS NULL
Image img = (Image)gvStory.Rows[row].Cells[0].FindControl("Image1");
string url = img.ImageUrl;
// string fileName = Path.GetFullPath(url); // RETURNS NULL
//string fileName = Path.Combine(Server.MapPath(#"Story/Food Fit For A King"), imageName);
File.Delete(fileName);
GetImage();
}
Am I doing the correct way to the the filepath of the image? But I need the full path of the image to delete it, I tried to use Path.GetFullPath(url) , it doesn't work. Need help on this.
And heres the aspx html side:
<asp:TemplateField HeaderText="Images">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("FileName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FileName") %>' Width="240" Height="160" />
</ItemTemplate>
</asp:TemplateField>
Need help on this.
If gridview row will be in edit mode, then only you can find the control that are inside <EditItemTemplate> tag. in editmode, you will get rowindex as -1. in other mode, rowindex will be greater than -1, then you can find controls that are inside <ItemTemplate> tag. Otherwise you will get null values. So, you can try this way,
protected void gvStory_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gvQuestion = (GridView)sender;
int row = e.RowIndex;
string fileName ="";
if(row==-1)
{
string imageName = (TextBox)gvStory.Rows[row].Cells[0].FindControl("TextBox1");
fileName = Path.Combine(Server.MapPath(#"Story/Food Fit For A King"),imageName);
}
else
{
Image img = (Image)gvStory.Rows[row].Cells[0].FindControl("Image1");
string url = img.ImageUrl;
fileName = Path.GetFullPath(url);
}
File.Delete(fileName);
GetImage();
}
My application is an image gallery and with a Repeater control i'm listing the thumbnails (that's in a separate folder, apart from the full scale images). When clicking on a thumbnail a full scale image should be shown in the Image control "fullSizeImage" and a query string should be created which (with a GET of the page) shows that specific image in full scale.
The code for the query string is done, but the problem is that I don't have a clue where to put it (the creation of the query), because the HyperLink control doesn't support event clicks. Is there a way to use for example Repeater ItemCommand, or how could I accomplish what I want here?
Thanks!
from default.aspx:
<asp:Image ID="fullSizeImage" runat="server" />
<asp:Repeater ID="ImageRepeater" runat="server" DataSourceID="" >
<ItemTemplate>
<asp:HyperLink ID="ImageHyperLink" NavigateUrl='<%# Eval("Name", "~/Images/{0}") %>' runat="server" CssClass="thumbnails" >
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~/Images/Thumbnails/{0}") %>' CssClass="thumbnail" />
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
from code behind:
protected void Page_Load(object sender, EventArgs e) {
var directory = new DirectoryInfo(Gallery.PhysicalApplicationPath + "/Images");
var theFiles = directory.GetFiles();
ImageRepeater.DataSource = theFiles;
ImageRepeater.DataBind();
var dataName = Request.QueryString["name"];
fullSizeImage.ImageUrl = dataName;
}
the creation of the query string (that I don't know where to put):
string str = ImageUrl; <- the url of the clicked image
Response.Redirect("default.aspx?name=" + Server.UrlEncode(str);
This works with me
<asp:HyperLink ID="ImageHyperLink" NavigateUrl='<%# "~/default.aspx?name=" + Server.UrlEncode(Eval("Name","~/Images/{0}")) %>' runat="server" CssClass="thumbnails" >
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~/Images/Thumbnails/{0}") %>' CssClass="thumbnail" />
</asp:HyperLink>
In the code behind you can set up a method tied to the Repeater's ItemDataBound event. In that method you can retrieve the current file, find the HyperLink, and set the link's NavigateUrl to be the string you are generating. Something like the following:
ImageRepeater.ItemDataBound += new RepeaterItemEventHandler(ImageRepeater_ItemDataBound);
private void ImageRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
[File] f = (File)e.Item.DataItem;
HyperLink ImageHyperLink = (HyperLink)e.Item.FindControl("ImageHyperLink");
string str = f.ImageUrl;
ImageHyperLink.NavigateUrl = "default.aspx?name=" + Server.UrlEncode(str);
}