views:

1458

answers:

4

OK I have a GridView and there is a column that I want to be a link if a file exists, otherwise I just want it to be a label. Right now I am changing the controls on RowDataBound event handler using the Row passed in the args. I am not a big fan of this as I am hard coding the column ID, and if it ever changes I will need to remember to change this code. I was hoping I could do a conditional in the asp code to add a link if a property value is not null otherwise add a label. Is this possible? Any different solutions?

I would like something like this:

<asp:TemplateField HeaderText="Status">
   <ItemTemplate>
    <%# if (Eval("LogFileName") == null)
    <%#{ 
          <asp:LinkButton ID="LogFileLink" runat="server" CommandArgument='<% #Eval("LogFileName") %>' CommandName="DownloadLogFile" Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
    <%# }
    <%# else
    <%#{
          <asp:Label ID="LogFileLabel" runat="server"Text='<%# Blah.NDQA.Core.Utilities.GetEnumerationDescription(typeof(Blah.NDQA.Core.BatchStatus), Eval("Status")) %>'>
          </asp:Label>
    </ItemTemplate>
</asp:TemplateField>
+1  A: 

You can continue to use RowDataBound event but in your aspx you add:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

In your C# code something like that:

if (LogFileName) {
  LinkButton ctrl = new LinkButton();
  ctrl.CommandArgument= ...;
  ctrl.CommandName= ...;
} else {
  Label ctrl = new Label();
  ctrl.Text= ...;
}

// You have to find the PlaceHolder1
PlaceHolder1.Controls.Add(ctrl);

In this way you don't have to hard coding the column ID

RioTera
A: 

Use properties on the page to determine if you want to show the label or the link

<asp:GridView ID="gv" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:LinkButton runat="server" Visible='<%# ShowLink %>' PostBackUrl="~/Aliases.aspx" >This is the link</asp:LinkButton>
                    <asp:Label runat="server" Visible='<%# ShowLabel %>'>Aliases label</asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

The add the properties ShowLink and ShowLable toyour code behind

public bool ShowLabel
    {
        get
        {
            //determine if the label should be shown
            return false;
        }
        private set
        {
            //do nothing
        }
    }
    public bool ShowLink
    {
        get
        {
            //determine if the link should be shown
            return true;
        }
        private set
        {
            //do nothing
        }
    }
cptScarlet
I have thought of that too...just did not really want to add data to my model for the UI.
CSharpAtl
+1  A: 

If you're going to be doing this a lot, I suggest writing your own field. The simplest approach is probably to make a NullableHyperlinkField inheriting from HyperlinkField, and render out a plain string if the anchor's URL would otherwise be null.

Dave Roberts
A: 

I know this is a little old now but just in case someone else stumbles across this as I did when looking for an answer to a similar question, I found you can do something like this:

<ItemTemplate>                      
    <asp:ImageButton ID="btnDownload" runat="server"
     CommandName="Download"
     CommandArgument='<%# Eval("Document_ID") & "," & Eval("Document_Name") %>'
     ImageUrl="download.png" ToolTip='<%#"Download " & Eval("Document_Name") %>'
     Visible='<%# Not(Eval("Document_ID") = -1) %>' />
</ItemTemplate>

i.e. set the Visible property to evaluate a boolean expression based on your field. If you wanted to display something instead of the download link or button, such as a "Not available" label, then you would just set its Visible property to the opposite boolean expression to your download link. (This is VB.NET not C#, but you get the idea.)

Jen