Only show hyperlink text in asp.net 2 gridview if database column is not empty

B

Bren

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 etc.
i.e Secratary loads the page on the intranet, finds the manager they want to
contact, clicks on the "SMS" hyperlink and is taken to the SMS page to fill
out the message and send it to the manager.

My issue is that not all managers have mobile phones so I only want to show
the "SMS" hyperlink text if the "mobile" database column has a number in it.

I am pretty sure the Eval() function with a template field is what I need
but I am unsure how to use this so some pointers and sample code would be
very much appreciated here.

TIA
Bren
 
D

David Wier

What you need is a 'helper' function, to check the data when it is returned
from the database -
Function CheckData(sItem as text) as text
if sItem is System.DBNull.Value Then
CheckData=""
else
CheckData= ' build your hyperlink here
end if
End Function

Then, put the function around the eval statement - something like:
CheckData(Eval(YourDBfield))

This is all ottomh, so it could take a little tweaking
:)
 
B

Bren

Hi David

After a bit of tweaking with your suggestion I got it to work as follows:-

In the code behind page I created the following function
Function CheckMobile(ByVal sItem As String) As String

If Len(sItem) > 1 Then

CheckMobile = "SMS"

Else

CheckMobile = ""

End If

End Function

And then in the webform Itemtemplate I did this

<ItemTemplate>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
Eval("mobile", "~/smscenter/Default.aspx?mob={0}") %>'

Text='<%# CheckMobile(Eval("mobile", "")) %>'></asp:HyperLink>

</ItemTemplate>

Now works a treat

Thanks very much for your help

Rgds
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top