Converting Boundfield to hyperlink with if statement - asp.net

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.

Related

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>

Get Image from database by clicking on link button in gridview in asp .Net

I have LinkButton in grid view
<asp:TemplateField HeaderText="Images">
<ItemTemplate>
<asp:LinkButton ID="lnkDrvImgLic" CommandArgument='<%# Eval("intVou") %>' CommandName="viewLicImg" runat="server">Licence Image</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%#Eval("intVou") %> is Primary key and it should show the image from database based on primary key. Does it requires any parameter for server path?
It would be fine if I can open image in new tab.
Or you can instead use the Hyperlink control like this:
<asp:HyperLink NavigateUrl='<%# String.Concat("~/",Eval("ImagePathField")) %>' Target="_blank" runat="server" >L</asp:HyperLink>
Notice that you must use "~/" to get the relative path for the image. Also you need to add Target"_blank" to open the image in new tab.
Hope this helps.

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 ''

Custom validation not working with delete buttons?

In a ListView, I have a CustomValidator set up to validate a field whenever a button with CommandName="Delete" is clicked.
<ItemTemplate>
<asp:TextBox ID="NameTextBox" Text=<%# Eval("Name") %> runat="server" />
<asp:Button ID="DeleteButton" Text="Delete" CommandName="Delete" ValidationGroup="Delete" runat="server" />
<asp:CustomValidator ValidationGroup="Delete" SetFocusOnError="true" Display="Dynamic" OnServerValidate="CustomValidator_ServerValidate" runat="server">You can't delete this.</asp:CustomValidator>
</ItemTemplate>
However, the error message is never displayed and the processing continues. What's strange is that the custom validation method is called, finds the field, and properly sets up e.IsValid to false. It does not matter whether I check Page.IsValid or not, because the error message is not displayed anyway.
It works if I remove the CommandName="Delete" from the button.
With Google I found the following solution, which seems to indicate someone has had a similar issue:
http://devio.wordpress.com/2007/12/11/formview-delete-button-and-customvalidators/
But I want to make sure that this solution is the way to go. I mean, is custom validation really not supposed to work with a delete button in a databound control, seriously?
I've alrealy heard about a problem like that, he solved it by doing it entirely differently. Like, instead of the customValidator, he puts a Label set EnableViewState="False" and Visible="False" and he check on the delete event the conditions and put the response back into the label. Maybe it can't works for you too?
But, if you realy ask "Why?????", I know he didn't find the exact reason ...

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() %>

Resources