473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hyperlink in Gridview

I have wasted a couple of hours trying to add a hyperlink to a templatecolumn in a gridview. I already have a HyperlinkField working in the gridview, but I would like nothing to be displayed if the DataTextField associated with the field is blank. Right now it is showing a "broken" url. I would just like to add a condition that says if this DataTextField is not DBNull, show the hyperlink, otherwise show nothing. Any pointers would be appreciated...
Mar 23 '09 #1
8 7678
tlhintoq
3,525 Expert 2GB
Please show the code you have thus far so we can all be on the same page
Mar 23 '09 #2
This is a column within a GridView. The problem is that I want the hyperlink to be blank if TEMP is DBNull. In the destination page I grab the querystring value for TEMP and query SQL server.

Expand|Select|Wrap|Line Numbers
  1. <asp:HyperLinkField DataTextField="TEMP" DataNavigateUrlFields="TEMP"
  2.                      DataNavigateUrlFormatString="Update.aspx?TEMP={0}" HeaderText="TEMP" 
  3.                     HeaderStyle-ForeColor="#ff9100" ControlStyle-Width="40"/>
Mar 23 '09 #3
tlhintoq
3,525 Expert 2GB
I'm not an ASP guy, but concepts come before language.
Can't you get the result of your query before you stick it in the gridview?
If the result != null then do what you're doing
else don't.
Mar 23 '09 #4
That probably doesn't meet my needs because I have several GridViews showing similar data and this is only one field. I need this to happen when binding occurs to the actual GridView columns.

I was hoping to do this with an Eval type statement as this is not the only field in the Gridview. For some reason I was not able nail down the correct coding for it (not having done it before). I am evaluating dates and other fields, but nothing in a HyperlinkField.
Mar 23 '09 #5
tlhintoq
3,525 Expert 2GB
Could you do something with the ? construct?

variable = ConditionToEvaluate ? ReturnedTrueValue : ReturnedFalseValue ;

Where the condition to evaluate is the link and if its null return string.empty and if not null then return a real string for the link?
Mar 23 '09 #6
Frinavale
9,735 Expert Mod 8TB
In your Server Code implement a method that handles the RowDataBound event and set the URL of the hyperlink by hand there.
Mar 23 '09 #7
Try this code



Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="gvdata" runat="SERVER" AutoGenerateColumns="false">
  2.     <Columns>
  3.     <asp:BoundField DataField="benefit_id" HeaderText="ID" />
  4.  
  5.         <asp:TemplateField>
  6.             <ItemTemplate>
  7.                 <asp:HyperLink ID="hyper" runat="server" Text='<%#eval("benefit") %>' ></asp:HyperLink>
  8.  
  9.             </ItemTemplate>
  10.         </asp:TemplateField>
  11.     </Columns>
  12.   </asp:GridView>

VB Code


Expand|Select|Wrap|Line Numbers
  1. Protected Sub gvdata_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvdata.RowDataBound
  2.         If e.Row.RowType = DataControlRowType.DataRow Then
  3.             Dim hyper As HyperLink = CType(e.Row.Cells(1).FindControl("hyper"), HyperLink)
  4.             If hyper.Text = "" Then
  5.                 hyper.Text = ""
  6.             Else
  7.                 hyper.Text = "asdsa"
  8.  
  9.             End If
  10.         End If
  11.     End Sub
Mar 24 '09 #8
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 24 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
3
by: Wayne Wengert | last post by:
I am trying to impliment the extensions to the Club Starter kit as described at: http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/extendclubwebsitesk.asp but when I...
1
by: Carlos | last post by:
Hi all, I need a field that it is both databound, and hyperlink in a gridview. That is, the value is obtained from a query, and at the same time needs to point to a new page where will be used...
3
by: Bren | last post by:
Hi All VS2005 Gridview control with vb I am populating a gridview of company staff. One of the columns is a hyperlink to a SMS texting facility I have so secrataries can text the managers...
1
by: willip | last post by:
I have a Gridview control with a Databound Hyperlink. e.g.: <asp:HyperLinkField HeaderText="MyTitle" DataNavigateUrlFields="MyID" DataNavigateUrlFormatString="~/ShowMe.aspx?MyID={0}"...
3
by: William LaMartin | last post by:
I have a gridview (with no properties set) on an aspx page which I populate from an XML file with the code below. The data in the XML file looks like this <description>National Trust for...
7
by: David | last post by:
Using ASP.Net 2.0 and VS 2005. I have a search page that has server controls, (textboxes, dropdownlists, etc.) that are used for searching and returning data in a GridView. One of the GridView...
4
by: =?Utf-8?B?QW1pciBUb2hpZGk=?= | last post by:
Hi I have a GridView that is displaying master records. Some of these records have child records. I would like to a column to my master GridView such that for each master record that has...
1
by: Paul W Smith | last post by:
The text below is a GridView I am trying to use to display some data from a database. I am using one templatefield of grid because I want the contents of the record fields to appear on three...
1
by: Author | last post by:
I got into trouble with this problem. The data I am presenting in a GridView control has a column called "Website". That data comes from a stored procedure in SQL Server 2000. Most of them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.