adding sql query output to hyperlink in asp.net - asp.net

I think it might have been asked before but i was unable to find the right answer, so i am asking here. i have added a data source which is working fine, i wanted a feature where i query the top n entries from the database and add it with a hyperlink. Think of it like Latest News! The markup for the hyperlink inside the ItemTemplate of DataList is this.
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>' NavigateUrl="~/News.aspx?NewsId=<%#Eval("News_Id") %> " runat="server" /> </asp:HyperLink>
however i get The error as "Error Creating Control, Server tag is not well formed". It reports the error where the quotes are placed.
I know i can use datanavigateurl property but i want to write it in this way. as written in the markup above. How can i?
Upon re writing it to
NavigateUrl='~/Product.aspx?DVDID=<%#Eval("Title") %> '
i get the following as the url
http://localhost:61221/Product.aspx?DVDID=<%#Eval("Title") %>

try this :
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>'
NavigateUrl='<%#Eval("News_Id", "~/News.aspx?NewsId={0}") %>'
runat="server" />
</asp:HyperLink>
<%# Eval() %> must be inside single quotes, otherwise it throws error.
To concatenate string in your binding tag, you can use this :
<%# "~/News.aspx?NewsId=" + Eval("News_Id").ToString() %>

Related

Why is <%# Eval(...) %> not rendering? Actually shows "<%# Eval(...) %>" in the client

I have the following control in ASP.Net
<asp:Button ID="btnDownload" runat="server" Text="Run" CssClass="button" OnClientClick='Download( <%# Eval("ID") %> )' />
When I run the page, the following details are being outputted to the browser.
<input type="submit" name="gvMaster$ctl02$btnDownload" value="Run" onclick="DownloadReport( <%# Eval("ID") %> );" id="gvMaster_btnDownload_0" class="button" />
Why isn't the <%# Eval("ID") %> actually evaluating? To be clear, originally I had a small JS snippet within this statement. I had to make a minor modification, so I moved it to a JS method. After moving the code and leaving just the ID as a parameter, this result started showing up where, before, it was actually evaluating to the value in the associated record.
What could be going on? I've not seen this before.
Reposting comment as answer:
I don't think you can set properties on server side controls using data-binding like that (mixing static text and dynamic Eval values). You should use the OnItemDataBound event for the parent repeater/datagrid/etc and set it in the code behind, or use OnClientClick='<%# Eval("ID", "Download({0})") %>', where the entire value is evaluated and sent to the attribute all at once.
try OnClientClick='<%# "Download(" +Eval("ID") + " );" %>' i suspect you need the server code to evaluate the whole expression in javascript so when rendered by the browser it will correctly trigger the function.

If statement, or some conditional inside asp:repeater BuildLink (multiple evals)

I need to make some sort of conditional when the eval returns nothing. Currently it creates a link "My Link" That leads to nowhere when the url is blank. I would prefer the "My Link" to not show up at all when the url is blank.
I've tried to implement something like what is found in the first answer to this question...#Eval if statement in repeater but either the buildlink() or the multiple eval() statements are throwing me some errors.
Here is the code I currently have
<asp:HyperLink runat="server"
NavigateUrl='<%# BuildLink(Eval("TaskDefinition.Url").ToString(), Eval ("TaskInstanceID").ToString())%>'>
My Link
</asp:HyperLink>
you need to add visibility attribute
<asp:HyperLink runat="server"
NavigateUrl='<%# BuildLink(Eval("TaskDefinition.Url").ToString(), Eval("TaskInstanceID").ToString())%>'
Visible='<%# String.IsNullOrEmpty(BuildLink(Eval("TaskDefinition.Url").ToString(), Eval("TaskInstanceID").ToString())) %>'
>My Link
</asp:HyperLink>

The server tag is not well formed, ASP Repeater Datasource

I've been getting parser error with message The server tag is not well formed for the following line.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>">
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1" runat="server" FieldName="Tag name" Item="<%# Container.DataItem %>"/>
</ItemTemplate>
<SeparatorTemplate>
/
</SeparatorTemplate>
</asp:Repeater>
The syntax looks fine, but one thing I'm not sure about is whether you can use the ".Field["tags"] element in there.
I've tried looking it up, but couldn't find a similar problem. I'm hoping someone provide me with some insight to why the parser is complaining about this line.
Thanks
What comes into my mind right now is to use a single-quoted string instead:
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
You have double quotes within the attribute. This confuses the parser - it can't tell where the attribute ends.
Wrap the attribute in single quotes to fix it:
DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>'
try ' instead of " it might work
else try binding from code behind
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
Do you have a closing tag? i.e.
</asp:Repeater>
Otherwise you are missing the / at the end of your tag declaration.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>" />

Adapting a jQuery slideshow to work with an ASP Repeater

I'm using a jquery slideshow and I want to fill in the pictures from a database. I'm trying to use an ASP repeater to create the images in a div with this code.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="dsSelectedCategory" Visible="True">
<ItemTemplate>
<a><asp:Image ID="Image2" runat="server" ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>" class="slide" /></a>
</ItemTemplate>
</asp:Repeater>
I'm getting the error that the server tag is not well formed and I'm guessing it is this bit
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
PictureFilePath is a field in the database that holds the filename and extension of the file. So I have to write out the path to the file then add on the name.
Try
ImageURL='<%# "~/img1/photos/" + Eval("PictureFilePath") %>'
I have tried your code and found the answer. Finally, it solved my problem as well. Just mark as answer if worked for you.
This is your old code
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
change it to
ImageURL='~/img1/photos/<%#Eval("PictureFilePath") %>'
replace the double quotes "" with single quotes ''

Dynamically assigning properties on a non data bound ASP.NET control

For example, let's say I have a HyperLink:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="foo.aspx" />
How can I set the NavigateUrl on the server side, without having to go the code-behind?
This doesn't work of course:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="<%= urlString %>" />
(where urlString might be a string I created earlier in the page)
And this doesn't work because the HyperLink is not within a data bound control:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl='<%# urlString %>' />
I guess I could just use a standard anchor element:
Foo
But I'd rather not mix up HTML and ASP.NET controls, and it would be handy to be able to do this for other controls.
Surely there must be a way?
Try setting the property in an inline code block:
<asp:HyperLink runat="server" ID="MyLink" Text="Foo" />
<% MyLink.NavigateUrl="foo.aspx"; %>
This doesn't work of course:
Of course it does.
And this doesn't work because the
HyperLink is not within a data bound
control:
Consider the Page as your data bound control.
You need to calls it's DataBind method.
Page.DataBind();
Maybe you need to add an ID attribute as well. If that does not work, try to display a property instead of a variable.

Resources