filling <img src="?"> dynamically in asp.net c# - c#

Hi would like to add img source path dynamically as shown in the snippet below, but giving error. I know img_src_path adding syntax is correct. Unfortunately I don't know the solution, need help.
Environment:
ASP.net, c#
<% string a[0]="image/hello.jpg;"
{
String img_src_path= a[0].ToString();%>
<li><a href='"<%#img_src_path%>"'><img src='"<%#img_src_path%>"' alt="" title=""/></a></li>
<%}%>
/Shashi

The problem is that your image tag does not have a runat="server" attribute (you will also need to add an ID in order to do this).
You also need to change your server tags like this:
FROM:
<%# ... %>
TO:
<%= ... %>
Also, the proper way to do this in ASP.Net would be to use the Image server control.
<asp:Image id="Image1" runat="server"></asp:Image>
Then, you would set the NavigateUrl property.

replace your code with this:
"=" instead of "#"
the solution
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' alt="" title=""/></a></li>
and if you want to edit image src form code behind
solution 2:
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' id="myImage" runat="server" alt="" title=""/></a></li>
edit image source form codebehind:
myImage.src = "imagePage";

The basic error we are trying to workout is Server tags cannot contain <% ... %> constructs.

Related

C# - I can't access my Master Page's img tag's src attribute from my code behind

<td>
<a runat="server" href="~/url.aspx">
<img src="<%= ResolveClientUrl("~/images/image1") %>" id="submissions"
border="0" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"></a></td>
When I try to run this code with runat="server" added to my img tag, I get a Parser Error that says "Server tags cannot contain <%...%> constructs." The C# code I tried in my code behind's Page_Load is:
if (Request.Url.AbsoluteUri.Contains("submissions"))
submissions.Attributes["src"] = "~/images/image3";
The goal is to highlight the part of the navigation bar that corresponds to the page the user is already visiting. The problem is that it doesn't allow me to access the img tag's src attribute.
Try
<img src='<%= ResolveClientUrl("~/images/image1") %>' id="submissions" ...
Pay attention to ' not "
If you need access to this tag from CodeBehind - the easiest way is to replace <img/> to <asp:Image runat="server" id="submissions"/> and in Master codebehind access by ID, but in child page - using
Image submissions = (Image)this.Master.FindControl("submissions");
Pay attention to right escaping here:
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl(\'~/images/image2\') %>',1)"

Name mangling for nested Master Pages

I use asp.net 4 and C#.
I have some nested Master Pages; I display in my Content Page a list of Links using a repeater.
This is a sample of code generated by ASP.NET as read in the Source code in the Browser.
As you can see the ID is very lengthy.
My question:
How can I have control on the ID generated, so I can chose another format much shorter?
Please keep in mind that I cannot get rid of Master Pages for my layout.
Thanks for your help on this!
<li>
<a id="ContentBody_ContentColumn2_latestArticle_uxRepeaterLatestArticles_uxLink_0" href="Category.aspx?CategoryId=8">AAAAA</a>
</li>
<li>
<a id="ContentBody_ContentColumn2_latestArticle_uxRepeaterLatestArticles_uxLink_1" href="Category.aspx?CategoryId=12">BBBBB</a>
</li>
I would like instead an ID like:
ID="CB_CC_LA_R_0"
ID="CB_CC_LA_R_1"
Useful article:
http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx
Replace the asp:HyperLink with plain HTML anchor tag and use following markup for it:
<a id='CB_CC_LA_R_<%# Container.ItemIndex %>' href='<%# Eval("IndexPropertyName", "Category.aspx?CategoryId={0}") %>' >
<%# Eval("TextPopertyName") %>
</a>

How to create a list of links in c#?

I'm trying to create a simple hyperlink list with a RepeaterItem (I'm not particular to a RepeaterItem, so if there are better ways...).
I'm pretty much using the code from the MSDN documentation linked above, but I have a simple problem, I'm using the <% %> control wrong:
Parser Error Message: The server tag is not well formed.
<li><asp:HyperLink id="navListItem" runat="server"
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "Url") %>">
<%# DataBinder.Eval(Container.DataItem, "Text") %></asp:HyperLink></li>
Apparently I cannot use <% within another asp.net tag.
What would be the "correct" way to create a list such as:
<ul>
<li>Link Text 1</li>
<li>Link Text 2</li>
<li>Link Text 3</li>
</ul>
The Url & Link text I get from a resource file.
You could either remove the double quotes for the NavigateUrl property or use a single quotes there instead:
NavigateUrl=<%# DataBinder.Eval(Container.DataItem, "Url") %>
or
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Url") %>'
and this should work.

data binding to html tag in asp.net

I was just wondering if it is possible to bind data using DataBinder.Eval on a html tag with runat=server attribute. For example i want to do something like:
<a href=<%#DataBinder.Eval(Container.DataItem, "file_name") %> runat="server" />
but it doesn't work. does this mean i have to use the asp.net hyperlink control?
Cheers,
Stephen
try this instead:
<a href='<%#Eval("file_name") %>' >Link text goes here...</a>
It works if the element is inside a bindable control, like a DataList or a GridView, and you don't need server control:
<a href='<%# Eval("file_name") %>' />

Customized bulleted list items in ASP.NET

I am just a beginner in ASP.NET. My question is simple, I wanna add list items dynamically from the code behind file and I want each item to have a text and couple of images as hyperlinks. The HTML sample should be like,
<ul>
<li>do foo <img src="some_image.png" /></li>
<li>do bar <img src="some_image.png" /></li>
...
</ul>
The number of items is dependent on the collection retrieved by the code behind file.
P.S. my code behind file is written in C#
The Repeater control is the simplest way to create a customized bulleted list, plus it gives you complete control over the HTML you generate. To use it, set up a template like this:
<ul>
<asp:Repeater runat="server" ID="ListRepeater">
<ItemTemplate>
<li>do foo <a href='#'><img src='<%# Eval("ImageSource") %>' /></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
Then in your code-behind (or declaratively in your markup, depending on your preference), set the repeater's data source and bind it:
void Page_Load(object sender, EventArgs e) {
// Some method you've defined to get your images
List<string> imageList = GetImages();
ListRepeater.DataSource = imageList;
ListRepeater.DataBind();
}
ASP.NET renders the template once for each item in your data source.
The Repeater control has more features than what I've shown here, but this should get you started. Good luck!
Edit: a year after writing this answer, I still think repeaters are the best option among server controls, but more and more I prefer foreach statements right in my .aspx templates:
<ul>
<% foreach(Image image in this.Images) { %>
<li>do foo <a href='#'><img src='<%= image.Source %>' /></a></li>
<% } %>
</ul>
Just use the Repeater control. Simply and easy. :)
ASP.Net BulletedList. MSDN

Categories

Resources