How do I conditionally display text in an ASP TemplateView? - asp.net

I have a few fields that look like this in a website that uses ASP and VB (the data is displayed in a gridview):
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server"
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
And what I'm trying to do is display nothing in the field if the data is empty, and display the string you see in the Text property if there is data. Currently it displays the hyphen used in the Text string when there is no data. I tried several methods of formatting the Eval that I found online but was unable to find a working solution. I also tried using the
EmptyDataText
property however this seemed to have no effect.
I am new to ASP so that could be user error. Any help is greatly appreciated.

You can also use eval for visible and check for data
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server" visible='<%# If(String.IsNullOrEmpty(Eval("CDE_CMPT")), false, true)'
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
I haven't used VB.net is a while, so the syntax might be off.

Related

Gridview Hyperlinkfield using DataNavigateUrlFormatString with Ampersand

So I have gridview pulling fields from a table and my hyperlinkfield is used to go to a specific page for that row to get more detailed data. Everything seems to work great except when the field used in the hyperlinkfield has an ampersand. I assume it is reading the ampersand as something else and so it doesn't bring up the proper info because the ampersand is in the name in the database.
Hyperlinkfieldcode:
<asp:HyperLinkField HeaderText="Name" Text="{0}" DataNavigateUrlFields="Name" DataNavigateUrlFormatString="item.aspx?name={0}" DataTextField="Name" />
Example:
A clicking on the name "test item" would take you to mysite.com/item.aspx?name=test%20item and this works.
However, clicking on "test & test item" it takes you to mysite.com/item.aspx?name=test%20item%20&%20item which does not work. It just pulls up a page with blank info.
What can I do to fix this?
Update:
I converted the hyperlinkfield to a hyperlink inside a template field, but the url is now coming out weird.the url now comes out like mysite.com/item.aspx?name=System.Int32%5b%5d
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Name") %>' DataNavigateUrlFields="Name" NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode({0}.ToString())%>' DataTextField="Name" />
</ItemTemplate>
</asp:TemplateField>
My original answer was incomplete (due to not being able to see the entire code). This answer will contain the missing elements.
The main object is to UrlEncode the data field Name, so that it can be used as (part of) a url link .
1 - First we must ensure that the field "Name", is listed as DataKeyNames for the GridView as follows:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name" ...
2 - Secondly (if using a navigateURL) create a TemplateField i.e. (asp:TemplateField ) and use Eval() method to access the DataField in conjunction with UrlEncode() .
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="NameLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode(Eval("Name").ToString())%>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Option 2 Alternatively you can use a HyperLinkField and DataNavigateURL but you should still designate Name as a DataKeyNames .
Hope this works with no problems. Let me know if any clarification is needed.
Happy coding and Cheers,

The server tag is not well formed in gridview

I have the hyperlink control on the GridView and I want to call the javascript function with passing parameters. I am getting Server Tag is not well formed error. I tried changing double quotes to single quote etc, still the same issue.
Can anyone help me find the issue here .
Line 1946: <asp:TemplateField HeaderText="Transaction Id">
Line 1947: <ItemTemplate>
Line 1948: <asp:HyperLink ID="lbltransId"
runat="server"
Text="<%# "<a href=\"javascript:subViewBookingDetails('"+
Eval("transId") +
"','','','','',,'','','')\">" +
Eval("transId") + "</a>" %>"></asp:HyperLink>
Line 1949: </ItemTemplate>
Line 1950: <FooterTemplate>
This should work. Do not use Text to populate link inside, use NavigateUrl instead.
<asp:HyperLink id="hyperlink1"
NavigateUrl="<%# String.Format(
"javascript:subViewBookingDetails({0} ,,,,,,,,)", Eval("transId"))%>"
Text="<%#Eval("transId") %>"
runat="server"/>
Text='<%# "" + Eval("transId") + "" %>'

Image not getting displayed in datalist

I am binding images to datalist. Taking the image name from database and giving the path.
My code is:
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" ImageUrl='<%# Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
On .cs page:
ds = gc.GetDataToListBinder("select DISTINCT PageOrderID,PageName from ScreenMaster order by PageOrderID")
dlImages.DataSource = ds.Tables(0)
dlImages.DataBind()
I am facing 2 problems :
When imagename has space in between it adds %20 in between
Eg. if imagename is "API Message", it takes it as: "API%20Message"
I tried On this Problem:
Added ImageUrl='<%#Server.HtmlDecode(Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg"))'
But I got error:
XML literals and XML properties are not supported in embedded code within ASP.NET
If there is not space Eg.image name is "Charges" , Then also its not showing it in datalist.
When i ran project, and right clicked on it and view source, then its showing me correct path as:
src="D:\Sagar\Kinston\WebSite\ScreenMasterImages\Charges.jpg"
but not showing image.
Please help me with above code.
Where i have made mistake?
What else i should add in it?
Keep it easy and simple.
When trying things like that with URL, to know exactly what to write down, try typing it in your address bar so you'll be able to find the exact syntax needed in order to make it work correctly.
I've done this tons of time and work for me... So you could try something like :
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px"
ImageUrl='<%# String.Format("{0}{1}.jpg", "~\ScreenMasterImages\", Server.HtmlDecode(Eval("PageName"))) %>'
runat="server" />
</ItemTemplate>
</asp:DataList>

Passing more than one command Arguments in gridview in asp.net

I have a TemplateField column in a gridview with a image button inside of it.
i need to use two command arguments within single image button.
here is my code
<asp:TemplateField ItemStyle-Width="30px">
<ItemTemplate>
<asp:ImageButton ID="btnAvaililabitly" runat="server"
ImageUrl="~/CMS/images/available_icon.png"
Width="12px" Height="12px" CommandName="availability"
ToolTip="Rooms Availability"
CommandArgument='<%#Eval("HotelID")%>'/>
</ItemTemplate>
</asp:TemplateField>
please help
You can send comma separated string and collect value on server side
CommandArgument= '<%# String.Format("{0} , {1}", Eval("Name1"), Eval("Name2")) %>'

Customize label text in grid view template field

I want to show "N/A" text in grid view label if value is not available in database and if it is available, then the value should be displayed instead of "N/A".
How can I customize my label?
This is the code that I have written to get the value.
<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# Eval("CineRunFrom") %>'></asp:Label>
This works:
<asp:Label id="dada" runat="server" Text='<%# string.Format("{0}",string.IsNullOrEmpty(Eval("CineRunFrom").ToString())?"N/A":Eval("CineRunFrom")) %>' ></asp:Label>
You may use this: Text='<%# Eval("CineRunFrom")?? "N/A" %>'
Add a new function in code behind & call it from HTML code, check sample code below.
Code
Private Function GetDisplayText(ByVal CineRunFrom As String) As String
'Do whatever you want here and return text to dispaly as required
End Function
HTML
<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# GetDisplayText(Eval("CineRunFrom")) %>'></asp:Label>

Resources