3

I have a GridView (gv) bound to a dataset (ds). Columns[1] is bound to a field in ds named orderFilename; Columns[6] is a date field.

If Columns[6] is null, I want Columns[1] to appear as text; if Columns[6] is not null, I want Columns[1] to appear as a hyperlink, with a url ~/directory/ + orderFilename.

I have found a couple possible solutions on the Web but none seem to do what I want. Any help would be appreciated.

2

3 Answers 3

4

I prefer to stay away from BoundFields specifically because the next guy needs to always seem to convert them to template fields anyways to do customizations. I would recommend the following:

Use a template field with a Literal control for your column 1:

<asp:TemplateField HeaderText="File">
    <ItemTemplate>
        <asp:Literal ID="ltFilename" runat="server" 
            OnDataBinding="ltFilename_DataBinding" />
   </ItemTemplate>
</asp:TemplateField>

Then implement the OnDataBinding for the columns control:

protected void ltFilename_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    if (Eval("yourColumn6Field") == DBNull.Value)
    {
        // just show a text filename
        lt.Text = Eval("orderFilename").ToString();
    }
    else
    {
        // produce the link
        lt.Text = string.Format("<a href='{0}'>{1}</a>",
             ResolveUrl("~/directory/" + Eval("orderFilename").ToString()),
             Eval("orderFilename").ToString());
    }
}

The advantage to this is you have localized the logic directly to the control. You can easily swap it out and change it around without affecting other parts of the grid accidently.

3
  • I'm trying to use this, but for some reason, the if statement is not reading the column field as null. Even though it says NULL in the db when I open it using SQL Server Manager, this is ending everything to the "else" block. Aug 19, 2011 at 18:32
  • @Dave Reinheimer Oops sorry Dave, you need to compare against: DBNull.Value not null :) I have updated the answer to reflect the correct compare.
    – Kelsey
    Aug 19, 2011 at 20:14
  • Found the answer here: linkstring.IsNullOrEmpty()...appears to be working now...just saw your comment Kelsey..I'll take a look...thanks! Aug 19, 2011 at 20:15
1

Let's say, you have added a hyperlink control in column[1], If the column[6] is not null then you can set the NavigateURL property and set the URL. In this case, it will look like a hyperlink and if column[6] is null, then you don't need to set the URL, as it will behave like text.

1
  • you have to be careful... It is rendered properly in IE, but in FF it shows up underlined still... so you will also need to set the style.
    – c0deNinja
    Aug 19, 2011 at 16:54
0

Use a template column, and put two panels inside of it. One panel containing the link, and the other containing the text. Try something like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Panel ID="pnlLink" runat="server" Visible='<%#Eval("SomeColumn") != null%>'>
             <asp:HyperLink ... ></asp:HyperLink>
        </asp:Panel>
        <asp:Panel ID="pnlLink" runat="server" Visible='<%#Eval("SomeColumn") = null%>'>
             <%#Eval("SomeColumn")%>
        </asp:Panel>
    </ItemTemplate>
</asp:TemplateField>

The other option, as @Muhammad Akhtar suggested, is to use a hyperlink regardless, and only set the URL when the DataField for Column[6] is not null.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.