Reduce or curtailment a displayed cells text in Gridview without wrapping text - asp.net

I have executed a database query that returns a long field data, in my case I need to curtailment one of this fields as the sample below.
How can I display a shortcuted content with add dots like(...)

try below query
select (CASE WHEN LEN(Name)>20 THEN SUBSTRING(NAme,0,20) + '...' ELSE Name END) AS Name from Table1

Something like this?
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("URL").ToString().Length > 20? (Eval("URL") as string).Substring(0,10) + " ..." : Eval("URL") %>' ToolTip='<%# Eval("URL") %> '> </asp:Label>
</ItemTemplate>
Adjust the length as you wish.

Related

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") + "" %>'

How to handle single quote in query string parameter values inside Eval for navigateurl property of hyperlink

Inside Gridview control
If my ID_Logon value has single quote characters in it, then the string gets terminated at the single quote.
for eg, if Id_Logon = O'connel
then only the O is being passed as a parameter. How to pass the whole string?
<asp:TemplateField HeaderText="LogonID" >
<ItemTemplate>
<asp:HyperLink ID="hyperlink1"
NavigateUrl='<%#"EditLogon.aspx?ID=" + Eval("ID_Logon")%>'
Text='<%# Bind("ID_Logon")%>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Try this
NavigateUrl='http://home/?<%# Eval("U_ID") %>'
or
NavigateUrl='<%# "http://home/?" + (string)Eval("U_ID") %>'
source asp:hyperLink NavigateURL and Eval functions

How do I conditionally display text in an ASP TemplateView?

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.

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>

How to update an ASP.NET label with SqlDataSource return value

On an ASP.NET page, I have a SqlDataSource configured with the following SELECT command:
SELECT AVG(Rating) FROM [Ratings] WHERE ([AlbumID] = #AlbumID)
How would I place that average value into a label?
You need to use FormView control to put the Label in.
Something like this:
<asp:formview id="formview1" runat="server" datasourceid="your-datasource-id">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# Eval("column-name") %>' />
</itemtemplate>
</asp:formview>
Replace your-datasource-id and column-name in the above code.
PS: you might need to alter the query to have a column-name for that one value:
SELECT AVG(Rating) AS "average" FROM [Ratings] WHERE ([AlbumID] = #AlbumID)

Resources