Hyperlink in Gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    Hyperlink in Gridview

    I have wasted a couple of hours trying to add a hyperlink to a templatecolumn in a gridview. I already have a HyperlinkField working in the gridview, but I would like nothing to be displayed if the DataTextField associated with the field is blank. Right now it is showing a "broken" url. I would just like to add a condition that says if this DataTextField is not DBNull, show the hyperlink, otherwise show nothing. Any pointers would be appreciated...
    Last edited by Frinavale; Mar 23 '09, 08:06 PM. Reason: Moved to ASP.NET Answers from .NET
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Please show the code you have thus far so we can all be on the same page

    Comment

    • stoogots2
      New Member
      • Sep 2007
      • 77

      #3
      Code As Requested

      This is a column within a GridView. The problem is that I want the hyperlink to be blank if TEMP is DBNull. In the destination page I grab the querystring value for TEMP and query SQL server.

      Code:
      <asp:HyperLinkField DataTextField="TEMP" DataNavigateUrlFields="TEMP"
                           DataNavigateUrlFormatString="Update.aspx?TEMP={0}" HeaderText="TEMP" 
                          HeaderStyle-ForeColor="#ff9100" ControlStyle-Width="40"/>

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I'm not an ASP guy, but concepts come before language.
        Can't you get the result of your query before you stick it in the gridview?
        If the result != null then do what you're doing
        else don't.

        Comment

        • stoogots2
          New Member
          • Sep 2007
          • 77

          #5
          That probably doesn't meet my needs because I have several GridViews showing similar data and this is only one field. I need this to happen when binding occurs to the actual GridView columns.

          I was hoping to do this with an Eval type statement as this is not the only field in the Gridview. For some reason I was not able nail down the correct coding for it (not having done it before). I am evaluating dates and other fields, but nothing in a HyperlinkField.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Could you do something with the ? construct?

            variable = ConditionToEval uate ? ReturnedTrueVal ue : ReturnedFalseVa lue ;

            Where the condition to evaluate is the link and if its null return string.empty and if not null then return a real string for the link?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              In your Server Code implement a method that handles the RowDataBound event and set the URL of the hyperlink by hand there.

              Comment

              • bhupinder
                New Member
                • Feb 2009
                • 32

                #8
                Try this code



                Code:
                <asp:GridView ID="gvdata" runat="SERVER" AutoGenerateColumns="false">
                    <Columns>
                    <asp:BoundField DataField="benefit_id" HeaderText="ID" />
                    
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:HyperLink ID="hyper" runat="server" Text='<%#eval("benefit") %>' ></asp:HyperLink>
                                
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                  </asp:GridView>

                VB Code


                Code:
                Protected Sub gvdata_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvdata.RowDataBound
                        If e.Row.RowType = DataControlRowType.DataRow Then
                            Dim hyper As HyperLink = CType(e.Row.Cells(1).FindControl("hyper"), HyperLink)
                            If hyper.Text = "" Then
                                hyper.Text = ""
                            Else
                                hyper.Text = "asdsa"
                
                            End If
                        End If
                    End Sub
                Last edited by tlhintoq; Mar 24 '09, 09:30 AM. Reason: [CODE] tags added

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

                  Comment

                  Working...