Solved

Hiding Content within a Gridview's Itemtemplate field

Posted on 2006-10-20
7
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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