Solved

Hiding Content within a Gridview's Itemtemplate field

Posted on 2006-10-20
7
489 Views
Last Modified: 2008-03-06
Hi All

I have a Gridview which is databound to a SQL DB table. One of the columns within my SQL table is called LINK and it contains an ID for each record. What I want to do within a Gridview column is be able to provide an ASp:Hyperlink with the ID appended as a query string which I can do no problem.

BUT!... How could I hide the ASP:Hyperlink if an ID isn't supplied?

Many thanks

Rit
0
Comment
Question by:rito1
  • 4
  • 2
7 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 17775196
If you have an ID that you are using from the DB it should be set as the primary key of the table and set to be an int value with Identity set to true. This will ensure all records have an ID and you won't have to worry about it. Is there some reason you cannot do this?
0
 
LVL 1

Expert Comment

by:stto3703
ID: 17775236
Bind the hyperlink's "Visible" field to the id.
0
 
LVL 1

Author Comment

by:rito1
ID: 17775321
Hi stto3703

Sorry to ask but how do you bind the hyperlink's visible property to the ID?

strickdd, its not a primary key... the ID is optional in this scenario.

Rit
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Expert Comment

by:stto3703
ID: 17776366
So "link" is a Column of your table with some fields being NULL, correct?

This would be the easiest way if you use VS2005 or VWD:
Edit your grid's columns. Convert LINK to a "Template Field"

Then go to edit Templates by clicking the small triangle on the top-right corner of your grid.

Select the Link item field, remove whatever Control is currently there, drag a Hyperlink and click on the Hyperlink's small triangle and select "Edit Databindings". If "Visible" is not part of the list select Show all Properties (not sure if this is the correct wording).

Now just select your DB field as "LINK". To format the Hyperlink change the Text binding and the URL. The url would be something like "mypage.aspx?value={0}" and {0} is whatever your link value is.
0
 
LVL 1

Accepted Solution

by:
stto3703 earned 500 total points
ID: 17776545
Here's how your TemplateField tag should look like after your done with the above: (of course my field names are different)
The item in bold is important

<asp:TemplateField HeaderText="links" SortExpression="links">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("links") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("qid", "test.apsx?{0}") %>'
                        Text='<%# Eval("qtitle") %>' <b>Visible='<%# DataBinder.Eval(Container.DataItem, "links").ToString()!="" %>' </b>></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
0
 
LVL 1

Expert Comment

by:stto3703
ID: 17776577
FYI: Bold is <b> .... </b>
0
 
LVL 1

Author Comment

by:rito1
ID: 17777177
Hi stto3703

Thank you for you guidance. Taking you code as is I found that it was hiding the LINK field data rather than showing it. I also couldn't get the hyperlink to work either only show the text. But with your guidance on syntax etc.. I managed to get it towork with the following:

<asp:TemplateField>
                <ItemTemplate>
      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "news.aspx?newsid=" & Eval("LINK") %>'
      Visible='<%# IsDBNull(Eval("LINK")).ToString = "False" %>' Text="Further Info..."></asp:HyperLink><br />
                </ItemTemplate>
            </asp:TemplateField>

Any guidance on making my code above better is always open.

Again, many thanks

Rit
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now