How to use ImageUrl for displaying images in asp.net? - c#

I'm trying to use an imagebutton inside a foreach loop. In order to access the path to my images, I need to pass an object to ImageUrl which contains the path to my images. I tried passing the guitar.image object but it is not displaying anything, only broken images. Please give solution on this problem. By the way, the reason why im using objects is because the path to my images is inside the database. Also i've already researched about Repeater and will use it in the future, but right now i wanted know if there is a solution without repeater.
<asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1'
Runat='Server'>
<% foreach (guitarBrand guitar in brandList) { %>
<table class="one-third">
<tr>
<th rowspan="3" class="guitarLogoHover"><a href="<%= guitar.page
%>"><asp:ImageButton ID="Image" runat="server" ImageUrl="guitar.image"
Height="300px" Width="300px" /></a></th>
</tr>
</table>
<% } %>
</asp:Content>

Make sure your string in guitar.image is a proper path, relative or absolute.
ImageURL can be
"images/image1.jpg"
or
"https://website.com/images/image1.jpg"
If you are using a relative path, keep in mind that adding a / to the front will have it start at the root of your site while having no / (like the example I gave above) will begin from your current page location.

The ImageUrl property will be translated to the src attribute. Make sure this path is a correct path to the specified file.

Related

Using asp:Literal text in html attributes

quick question. Is there a way to use asp:Literal text in an HTML attribute?
Example
<asp:Literal Text="hidden" runat="server" ID="ClassTag"></asp:Literal>
<tr class='<%= ClassTag %>' run="server" > </tr>
I am working on an overall solution to a repeater table row collapsing problem (asp:repeater collapsing table rows) for context, and this is perhaps the last thing I'm stuck on.
Please let me know. Thanks!
Not really, but are you just trying to set the class on the TR from code?
In that case, write the following the ASP.NET:
<tr runat="server" ID="CustomRow">
And in the codebehind set the class through the Attributes collection:
CustomRow.Attributes.Add("class", "[desired css class]");

Changing HTML element's style property from code behind

I have a HTML page which is dynamically generating by server. The application has an IDE to generate and design pages then deploy the server. The server displaying this pages in an iframe. We can use all c# methods as well as Page_Load and Page_PreRender events in pages. But I can't modify source code of the asp.net page (I mean can't add runat="server").
What I want to do, finding a html tag by css class (#form1 > span) before pre-render then add a new css property in code behind.
<form id="form1" action="DocumentViewer.aspx" method="post" autocomplete="off">
<span>
<table>
<tr>
<td></td>
</tr>
</table>
</span>
Without runat="server" you cannot access the control in code behind. Best way to do it is to inject the jquery script from code behind to do the same work.
Please take a look at this answer.

Show image from sql on non javascript link

Hi say of have a table of cars in my SQL database. That table has a column for the car make and a column called picture of type:
Picture(image, null)
I'm then displaying my cars in a repeater and so it might look like this:
<asp:Repeater id="carsRepeater" runat="server" DataSourceID="CarsDataSource>
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label id="carMakeLabel" runat="Server" Text='<%# Eval("Make") %>' />
</td>
<td>
Some how display a clickable image here
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</Footer>
</asp:Repeater>
What I'm wanting is in column two to get the picture of the car to display and make it so you can click on that picture and it will run a method in the code behind. Also I have this requirement where the picture click method mustn't require javascript to run.
Can someone please give me an idea of how I would go about this?
I'm currently thinking about putting the image somehow in a link but I'm not sure how to display the image. Maybe the asp:Image but that requires an ImageUrl.
Try image button, it displays an image, doesn't require javascript and you can run a method on click event from code behind
Write an HTTPHandler (say CarImage.ashx) and in that handler, given ID of the image return the binary data with ContentType of image/gif (or whatever). Render an image tag in the datagrid with src as CarImage.ashx?ImageID=xxxxx. The process is described many place including this one.

ASP.NET Conditional Markup Rendering According to Web.config Key

I have a key in web.config as -
<add key="IsDemo" value ="true"/>
I want to show/hide markup based on above web.config entry for a non-server html tag without using code behind file (as there is no .cs file and there are no runat=server controls).
Something similar to following pseudo code:
IF ( IsDemo == "true" )
THEN
<tr>
<td id="tdDemoSection" colspan="2" align="left" valign="top">
<.....>
</td>
</tr>
ENDIF
Does anyone know that we can write such conditional logic in .aspx markup?
Please help!!!
EDIT:
Section I'm hiding or showing have some data like username and password. So, I do not want user to use Firebug or Web Developer Tools to see hidden markup. markup should not go to client side.
The syntax for something like that would be
<% if(System.Configuration.ConfigurationManager.AppSettings["IsDemo"] == "true") %>
<% { %>
<!-- Protected HTML goes here -->
<% } %>
This assumes that the page is in C#.
You can firm this code up by being more defensive around the AppSettings retrieval e.g. what happens in the case where the value is null etc.
Solution:-
<% If (ConfigurationManager.AppSettings("IsDemo").ToLower().Equals("true")) Then%>
<tr>
<.....>
</tr>
<% Else%>
<tr>
<.....>
</tr>
<% End If%>
If I understand it right, you don't want to use server-side (aspx components, with runat="server" attribute) and just want to control display of html on aspx page then try this solution.
Create a property in codebehind file (or better still in some other config helper class):
//IN C# (OR VB) file
protected string Demo{
get{
return ConfigurationManager.AppSettings["IsDemo"]=="true"?
"none":"block";
}
}
In aspx page:
<tr style="display:<%= Demo%>;">
<td>blah blah</td>
</tr>

Selecting an element without an Id

I'm trying to select an element from my webpage...
I have inserted a control into my page and i need to set some values an element inside the control at pageload from c# code.. The thing is, as soon as I insert the control into the page... A prefix is appended to the id name... Because of that new name, my css definition won't be appended...
Is there any way to access this element from C# without the need to make it an Id?
Edit: To clarify what Im trying to do here. Im trying to make a generic control, which gets a width and height set to it's parameters. I need to set this manually to the element by adding a style attribute. I can't make the element an id, because this will stop the possibility of making it generic.
This is whats inside of the control... the fact is, I need the imageRotatorDiv to be a class instead of an id. Otherwise i can't use multiple image rotators on one page.
But how can I select a class in a page from c# code? Is it possible?
<div id="imageRotatorDiv" runat="server">
<div class="imageRotator">
<asp:Repeater ID="rprImages" runat="server">
<ItemTemplate>
<asp:Image ID="imgItem" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
you can define your style to a class name instead of id.
<asp:TextBox ID="MyText" CssClass="someclass" runat="server" />
html output
<input type="text" id="Something_MyText" class="someclass" />
css
.someclass { border:solid 1px red; }
In JQuery :
$("div[id$=MyDiv]").addClass("myDiv")
And you just need to define the myDiv CSS class. Or, to modify directly the style :
$("div[id$=MyDiv]").css("font-size", "14px");
JQuery details

Categories