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...
Hyperlink in Gridview
Collapse
X
-
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
-
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
-
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
-
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 SubComment
-
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
Comment