Solved

Hiding Content within a Gridview's Itemtemplate field

Posted on 2006-10-20
7
490 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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