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

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>

Related

Converting Boundfield to hyperlink with if statement

I've been stuck on this for a little while now and can't find any answers that are specific enough to help with my problem.
I have a grid that is bound, containing notes automatically generated for different members of our crm system. Some notes contain PDFs - (non-clickable that you have to copy and paste into another tab on the web browser to open), others do not and are just general notes. I need to be able to convert the note to a hyperlink IF the note contains a link.
So far, I have managed to get all of the notes to come up as clickable links however I really need the IF statement in there as well. I am a complete novice and so please excuse any small issues that do not impact this issue directly.
<asp:TemplateField HeaderStyle-Width="100px">
<ItemTemplate>
<asp:LinkButton ID="lnkPDF" runat="server" CausesValidation="False" CommandName="Ir" PostBackUrl='<%# "~/" + Eval("Note") %>' Text="Link to Voucher"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
So, you can achieve this behavior. I assume you are simply just asking for a conditional thing to populate the LinkButtons with url or leave them with the text only. I will now write the solution with different possibilities according to my understanding:
Method 1
So, with this way you can check if your bound DataTable has null values and if it has, you can simply return an empty string to the PostBackUrl property.
<asp:LinkButton ID="lnkPDF" runat="server" CausesValidation="False" CommandName="Ir" PostBackUrl='<%# Eval("Notes") == DBNull.Value ? "" : "~/" + Eval("Note") %>' Text="Link to Voucher"></asp:LinkButton>
Note: If you use this method you will still have a postback. And the links that were not fetched or were null the will still have respective anchor elements on the web pages that will take you nowhere.
Method 2
With this way you can tweak with Visible property of the element. by this way you will actually have LinkButton Controls that were not DBNulls
<asp:LinkButton ID="lnkPDF" runat="server" CausesValidation="False" CommandName="Ir" Visible='<%# Eval("Notes") != DBNull.Value %>' PostBackUrl='<%# Eval("Notes") == DBNull.Value ? "" : "~/" + Eval("Note") %>' Text="Link to Voucher"></asp:LinkButton>
Method 3
I will recommend using this one if you are not strict on having a server control and you want to have the 'Link to voucher' displayed on screen and you are not catching this control in your codebehind in OnRowCommand event.
<a href='<%# Eval("Notes") == DBNull.Value ? "javascript:void(0)" : "~/" + Eval("Notes") %>'>Link to voucher</a>
Reason: Simple and straight forward.

Eval property and extra text on a button's text property

I'm using an asp Button like follows:
<asp:Button id="someButton" runat="Server" Text='<%# Eval("Size") %>' />
This is great but before the Eval Property result I wanted to add the Text "Download " with possibly the units after it.
eg. Download 123KB.
Can someone please tell me how to go about this?
In this case it might be easier to set the text property of the button on the server in the page load event.
insted of button use linkButton.
**asp:LinkButton ID="HyperLink2" OnClick="HyperlinkSelectedCustomers_Click" runat="server" CommandArgument='<%#Eval("id") %>' ><%#Eval("selectedCustomers") %>/asp:LinkButton

Build a HyperLink in a GridView in ASP.NET webforms - Eval not working

I have a gridview with a template field that has a HyperLink:
<asp:TemplateField ItemStyle-Width="12%" HeaderText="VER" HeaderStyle-HorizontalAlign="Center" SortExpression="Ver" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" NavigateUrl="~/Admin/Teste/Teste.aspx?rac=<%#Eval('idApontamento')%>" runat="server">TEXT</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I am getting The server tag is not well formed. in the HyperLink line.
What should I do in order to directly build a querystring in a HyperLink ?
Build your hyperlink like this:
<asp:HyperLinkField HeaderText="Title"
DataTextField="Some Text"
DataNavigateUrlFields="idApontamento,CustomerID"
DataNavigateUrlFormatString="~/Admin/Teste/Teste.aspx?rac={0}&CustomerID={1}" />
Keep adding comma delimited values to the DataNavigateUrlFields property, and markup the DataNavigateUrlFormatString as you would string.Format()
I don't think you can embed an expression like that, you have to pick to give it all text, or all binding expression.
Thankfully, you can contatonate string in a binding expression. Try something like this:
NavigateUrl='<%# String.Concat("~/Admin/Teste/Teste.aspx?rac=", Eval("idApontamento")) %>'
You've got an extra double-quote after the pound (#) symbol. Does removing that help?

adding sql query output to hyperlink in 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() %>

asp:HyperLink build NavigateUrl within Repeater using XPATH data

I am using a repeater for some products I am listing.
I'm trying to build an asp:HyperLink NavigateUrl using both hardcoded text as well as XPATH data.
NavigateUrl='mypage.aspx?ID=<%#XPath("THEID")%>&name=<%#XPath("THENAME")%>'
Obviously this isn't working.
Does anyone know how to make this work?
This should work:
<asp:HyperLink
runat="server"
NavigateUrl='<%# string.Format("mypage.aspx?ID={0}&name={1}", XPath("THEID"), XPath("THENAME")) %>'
Text="some link"
/>

Resources