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>
Related
My project is an asp.net and it has a master page that contains a list
<ul id="navigation">
<li id="li1" runat="server" class="selected">Events</li>
<li id="li2" runat="server">Add Event</li>
<li id="li3" runat="server">Profile</li>
<li id="li4" runat="server">Friends</li>
<li id="li5" runat="server">Find Friends</li>
<li id="li6" runat="server">Schedual</li>
<li>
<asp:LinkButton ID="LogOutButton" runat="server" OnClick="LogOutButton_Click">Log Out</asp:LinkButton>
</li>
</ul>
The selected class (css class) has a picture this picture tells the user on which page he is. How can I change this class using javascript or C# when I navigate?
I don't have a good experience with javascript
document.getElementById("li6").className = "whatever";
Should work/
$("#li1").addClass("selected");
Will work.
Example
This is very simply in JavaScript/jQuery.
But if this is for navigation, meaning it needs to be updated when you display a new page, I would do this in the markup from the server.
You didn't say much about how you are serving this page. But both ASP.NET WebForms and MVC allow you to control the HTML served in a number of different ways.
You can set for all your li tags classes like this for example:
<li class="li">
... content of the tag
</li>
and in your javascript you can add to all elements with class "li" some other class:
$(".li").addClass("classYouWantToAdd");
and in the css file:
.li
{
... needed css for the class
}
here is another post that tells you how to get elements by classname
Selecting a div by classname
I'm getting a list of string from db like:
AddCustomer,
AddUser,
ListCustomer,
ListUser
These are the prefix of asp pages.
I need to hide and show certain pages in the page. Following is the html snippet:
<li>Customer Management
<ul>
<%if (AddCustomer) //how to check whether my string is present or not, is it possible?
{ %><li>Add Customer</li><%} %>
<li>List Customer</li>
</ul>
</li>
Have you tried writing method then calling it from aspx page?
<%# YourMethodName((string)Eval("AddCustomer")) %>
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.
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.
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