I stored a collection image paths in the Sql database and displayed on an asp.net page. And I used the following code to get path of any image that user click on it.
My problem return path of the first image with any click.
How can I solve it?
Default.aspx
<asp:ImageButton ID="ImageButton1" runat="server" Height="200" Width="125" ImageUrl='<%#Eval ("img") %>' OnClick="ImageButton1_Click" />
Default.cs
if (dt.Rows.Count > 0)
{
ViewState["img"] = dt.Rows[0]["img"].ToString();
}
you should filter to get related row. You can use Linq for Object and filter using where and select your field.
example- (suppose filter by column id):
dt.AsEnumerable().Where(r=>r.Field<int>("id")==4).Select(r => r.Field<string>("img"));
You can try using Server.MapPath("filename").
For more details refer : https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx
Also the implementation might be different in mvc.
as per your details looks like you only get the value of
dt.Rows[0]["img"].ToString() it is only return first path of your image always
you need to use for loop for exact solution.
Related
I want to create a website which will store 2 images in the "home" page, which will lead to 2 different pages. In the case, I'm using the Image object from the toolbox. When I'm trying to assign the ImageUrl, it don't appear to find anything at all, regardless of the format. I've tried with multiple photos, different folders, there's the result:
I've also tried to change it programatically in C#, not working, as well:
Image1.ImageUrl = #"Images/left.jpg";
Should I use the usual <img src=""/> for finding images? Thank you very much!
Right click on that image shown and choose "include in project" and then try to give imageUrl
If you are sure that your path value contains the appropriate path from the ImageFiles directory, then you should be able to use the following :
<asp:Image ID="Image1" runat="server" ImageUrl='~/ImageFiles/<%# Eval("path")%>' />
or use the integrated string formatting of the Eval method as shown here :
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("path", "~/ImageFiles/{0}") %>' />
From the code behind you could do something like below including application base URL some thing like below-
Image1.ImageUrl = #"~/Images/left.jpg"
Please refer the below discussion for more information -
Image isn't showing in Image and ImageButton
ASP.NET will automatically replace the ~ with your application's base
URL, because Image1 is a server control
I need to make a slideshow, on a webpage.I am not allowed to use javascript, it must be coded using c# and html using the razor syntax. I have an array of images, my lecturer suggested using switch statements, but I have been trying for weeks with no avail. I have tried, form posts, if statements, switch statements.
he will not give any more help with this.
this is my array and this is how I'm calling the variable, this works fine, however i cant figure out how to make it change when i click next or previous. I've removed all code from the buttons as nothing ive tried will work.
any help would be greatly appreciated
string[] images = {"images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpeg", "images/6.png", "images/7.png", "images/8.png"};
<img src="#image" width="250px" height="250px" /><br />
<button>Next</button>
<button>Previous</button>
You could make next and previous "buttons" be links that call a controller with the number of the next and previous page in the query string parameter
You would of course ideally use JavaScript for this.
An alternative, and IMO not the best way, is to do something like:
Previous
Next
...and then get your razor template to display the appropriate image:
#{
string imageID = Request.QueryString["imageID"];
// fetch the image based on this ID
// (insert your logic here to get your image)
string theImage = #fetch.your.image
<img src="#theImage" />
}
so started using a new method for dynamicly displaying images inserted into my database. This is the asp.net code
<asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval ("ImageUrl", "~/img/{0}") %>' Width="200px" PostBackUrl='<%#Eval ("ProductID", "Product.aspx?ProductID={0}") %>' runat="server" />
now, as you can see what it does is getting the "ImageUrl" from my database depending on what the ProductID is, and then shows that as the image url. Example in my folder /img/ i have a picture called "pic.jpg" then this code would make the image url "~/img/Pic.jpg" now how would i add a new image into the table from the backend? i would have to make a flileupload control upload the file to the "img" folder and then insert only the file name as the "ImageUrl" in the database table. How is this done? do you have any examples or can point me in the right direction?
thanks!
all answers appreciated!
ImageButton is actually a html input type image. So you should not use it for just display purposes. You can use Image control or even use html img tag.
Check this link for FileUpload control
http://asp.net-tutorials.com/controls/file-upload-control/
Check this link for Image control
http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_image
I need to use the asp control "ImageButton" in order to have access to the properties "OnClick" and "OnClientClick"
The problem is i cannot give any ImageURL since my Image is in Database.
I used this previously :
<telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%#Eval("Image") %>'
AutoAdjustImageControlSize="false" Width="90px" Height="110px" Enabled="true" AlternateText="pas d'image"/>
But i don't have any Datavalue property in ImageButton control...
How can i manage to do this using ImageButton?
thanks in advance
You can use a Generic Handler file and call that as your ImageUrl like:
<asp:ImageButton ImageUrl='<%# String.Format("Image.ashx?id={0}", eval("ID")) %>' />
Read more on how to do this here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129
The fact its an ImageButton makes no difference. It's the fact you want to render an image from image data type. I believe generating the image on the fly using a Generic Handler file is the most common way.
You can use handler for creating image from database or from binary format and this handler you can call from your imagebutton -> imageURL and it will show the image on the page.
You may provide a method that will return an image as stream. Your URL may be:
http://www.yourwebsite.com/Application/Images/GetImage?name=myImageName&format=png
Inside your site you'll provide a GetImage page to query the database and write in the output stream the image data (do not forget to set the mime type).
In .NET(C#), I'm loading an image. The src of the image is stored in the database.
I currently retrieve text from my db using this:
TextBox4.Text = reader["descr"].ToString(); // snippet
However, I want to know, how would I display an image?
Image1.Text= reader["img1"].ToString();
and then in my WebForm:
<asp:Image ID="Image1" runat="server" />
Use the ImageUrl property instead.
Try
Image1.ImageUrl = reader["img1"].ToString();
Have you tried Image1.ImageUrl = reader["img1"].ToString();
This one has been answered many times on so:
Basically you need to create an image handler to load the binary stream of the image to the browser. Also if you are going this route, please remember to use the cache header for each image so you are not pulling data from your database on every request.
ASP.NET [Image Handler]
Enjoy!