ASP.net putting a dataitem in a repeater - c#

I've got a:
Data repeater
Control in the repeater
And I'd like to put some values into the controls. At the moment I have a control in it as:
<asp:Repeater runat="server" ID="ArchiveEntryRepeater">
snip
<asp:HyperLink ID="HyperLink1" ToolTip="Comment on Some Entry Title Here" runat="server" NavigateUrl="~/Blog/7/How-to-know-when-this/Comments">
<strong><%# DataBinder.Eval(Container.DataItem, "Comments")%></strong> Comments
</asp:HyperLink>
snip
</asp:Repeater>
Which works fine, but I want to put
<%# DataBinder.Eval(Container.DataItem, "Title")%>
Into the NavigateURL property of the Hyperlink. Just putting it in doesn't seem to work!

Try enclosing your NavigateURL in apostrophes instead of double quotes to not confuse the ASP.NET parser:
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Title")%>' [...]>[...]</asp:HyperLink>
This might be your issue.

You can always bind on the back end using the event itemdatabound. It has some advantages especially where the data is not a neat view or needs some type of major manipulation.

Another option would be to loose the control and replace it with an html anchor control like this:
<asp:Repeater>
<ItemTemplate>
<a id="HyperLink1" title="Comment on Some Entry Title Here" href='<%# DataBinder.Eval(Container.DataItem, "Title")%>'
style="font-weight: bold">'<%# DataBinder.Eval(Container.DataItem, "Comments")%>'</a>
</ItemTemplate>
</asp:Repeater>
Once the server renders a hyperlink to the client there is generally no reason to have it "runat='server'" . The purpose is to navigate to another page so the overhead of using an asp:HyperLink vs. an nchor is wasted.

Related

<%= %> tag not working to display content inside an ASP Label control

Noob question.
Why does this not work into my .aspx file?
<body>
<asp:Label ID="Label1" runat="server" Text='<%=System.DateTime.Today.Day.ToString()%>' ></asp:Label>
</body>
It does display the <%=System.DateTime.Today.Day.ToString()%> string which is obviously not what I want.
Same result if I try to display the content of a code behind variable:
<asp:Label ID="label" runat="server" Text='<%= versionNumber %>' >
versionNumber being properly instanced and set into the code behind.
You cannot mix server controls with code blocks.
There are two ways to work around that limitation:
Just use <%=System.DateTime.Today.Day.ToString()%> without a Label around it
Use codebehind to set Label1.Text = System.DateTime.Today.Day.ToString();
The first way will display the date to the user, but you cannot further change it from codebehind.
The second way does enable you to alter the text from codebehind.
It is true you can't mix server controls with Code blocks,
If its compulsory for you to use Server side control, and you don't even want to set value from code behind then you can go for this solution.
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>
Similarly you can use code behind variable as follows ,
<asp:Label ID="Label1" runat="server"><%=versionNumber %></asp:Label>
If you really want to use a asp:Label
Use it as follows:
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>

How Add Anchor Tags by repeater and To Jump To Specific Location On A Page?

I want to use repeater to populate anchors and links to that anchors dynamically.
I know for statistic case we can do this:
Link Text
and the anchor :
<a name="anchor"></a>
Now, to create the link dynamically I use:
<asp:Repeater ID="LinkRepeater" runat="server">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" class="TopMenuBarLink" NavigateUrl='<%# Eval("Link")%>'>
<%# Eval("Title")%>
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
But when I run the program, on the website, use inspect element I get this for the links:
<a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="Mypath/MyAnchor">My title text</a>
How do I get it in the right form:
<a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="#MyAnchor">My title text</a>
Do you have a specific reason that you're using a server control for your hyperlink? why not use a normal anchor element.
<asp:Repeater ID="LinkRepeater" runat="server">
<ItemTemplate>
<a class="TopMenuBarLink" href='#<%# Eval("Link")%>'>
<%# Eval("Title")%>
</a>
</ItemTemplate>
</asp:Repeater>
I would try this (add the # symbol before the ASP.Net code bracket):
NavigateUrl='#<%# Eval("Link")%>'

Dynamic HyperLink

I have a HyperLink along with other controls such as Label etc in a GridView. The Label's in the GridView are dynamically populated like this:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
I am now trying to do something similar with HyperLink, as in:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='http://www.company.com?clientname=<%# Bind("ClientName") %>'>Client Name</asp:HyperLink>
This doesn't give me any errors, but the link becomes this:
http://www.company.com/?clientname=<%# Bind("ClientName") %>
instead of something like this:
http://www.company.com/?clientname=oshiro
Anyone know how to get the link to work properly instead of just outputing the asp.net code?
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ClientName", "http://www.company.com/?id={0}") %>'>Client Name</asp:HyperLink>

window.open() in <asp:Hyperlink> in GridView passing id

I am using <asp:HyperLink> in Gridview
<asp:HyperLink ID="hyperlink1" runat="server" ImageUrl="~/images/zoom.png" OnClick="javascript:window.open('ProductSummary.aspx?id=', 'open_window', 'width=640, height=480, left=0, top=0')"></asp:HyperLink>
I need to know that how to pass '<%# Eval("ProductId") %>' in query string
Probably the easiest way is to produce the entire OnClick event in the DataSource for the grid view, and bind to that:
<asp:HyperLink ID="hyperlink1" runat="server" ImageUrl="~/images/zoom.png"
OnClick='<%# Eval("PopupClickEvent") %>' ... />
Otherwise, it should still be possible to have
OnClick='<%# Eval("ProductID", "javascript:window.open('ProductSummary.aspx?id={0}', ...)") %>'
But the quotation escaping gets messy and confusing very quickly.
For reference, see http://msdn.microsoft.com/en-us/library/4hx47hfe(v=vs.110).aspx

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.

Categories

Resources