Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Hiding Content within a Gridview's Itemtemplate field

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
rito1
Asked:
rito1
  • 4
  • 2
1 Solution
 
strickddCommented:
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
 
stto3703Commented:
Bind the hyperlink's "Visible" field to the id.
0
 
rito1Author Commented:
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
Independent Software Vendors: 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!

 
stto3703Commented:
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
 
stto3703Commented:
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
 
stto3703Commented:
FYI: Bold is <b> .... </b>
0
 
rito1Author Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now