Optional display of a control in a GridView cell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    Optional display of a control in a GridView cell

    Okay, here another problem I'm having difficulty solving...

    I've got a cell in my GridView that I need to have a control optionally displayed. Specifically, I have a cell with a bound variable of type Label. In that same cell, I also want to display a button (unbound). I can do that with something like this:

    Code:
                       <ItemTemplate> 
                        <asp:Label ID="completed_date" runat="server" Text='<%# Bind("completed_date") %>'></asp:Label><br>
                        <asp:Button ID="MarkAsCompletedButton" runat="server" CommandName="MarkAsCompleted" Text="Mark As Completed" />
                       </ItemTemplate>
    Works great, but I only want to display the button if the "completed_date " label is blank (which, BTW, is stored as a string, not datetime). How do I do that?

    Robert
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by RobertTheProgra mmer
    Okay, here another problem I'm having difficulty solving...

    I've got a cell in my GridView that I need to have a control optionally displayed. Specifically, I have a cell with a bound variable of type Label. In that same cell, I also want to display a button (unbound). I can do that with something like this:

    Code:
                       <ItemTemplate> 
                        <asp:Label ID="completed_date" runat="server" Text='<%# Bind("completed_date") %>'></asp:Label><br>
                        <asp:Button ID="MarkAsCompletedButton" runat="server" CommandName="MarkAsCompleted" Text="Mark As Completed" />
                       </ItemTemplate>


    Works great, but I only want to display the button if the "completed_date " label is blank (which, BTW, is stored as a string, not datetime). How do I do that?

    Robert
    Try the same CSS stuff on your button that you did for hiding your index.
    It's a bit trickier though....durin g your RowDataBound event you have to grab the contents of the cell, cast it to a button and then set the CSS style for the button.

    Cheers!
    -Frinny

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      Hmm...

      I'm still relatively new to ASP.NET and web programming. Do you mind giving a little more detail about how I go about the task of "cast it to a button and then set the CSS style for the button"? The first part I've got. Thanks.

      Robert

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by RobertTheProgra mmer
        Hmm...

        I'm still relatively new to ASP.NET and web programming. Do you mind giving a little more detail about how I go about the task of "cast it to a button and then set the CSS style for the button"? The first part I've got. Thanks.

        Robert

        I'm not sure if you're using C# or VB.NET ...but basically in your RowDataBound event handler method for your GridView you would have something like:

        VB.Net code:
        [code=vbnet]
        Dim theButton As Button = CType(e.Row.Fin dControl("myBut ton"), Button) ' this finds the control and casts it into a button...
        If Not theButton Is Nothing Then
        theButton.style .add("display", "none")
        'or set the CSS....like
        'theButton.CssC lass = "noShow"
        End If
        [/code]

        C# code (please note that my C# syntax may not be 100% working...but here's the general idea)
        [code=cpp]
        Button theButton = (Button) e.Row.FindContr ol("myButton") ; // this finds the control and casts it into a button...
        If ( theButton != null)
        { theButton.style .add("display", "none");
        //or set the CSS....like
        //theButton.CssCl ass = "noShow";
        }
        [/code]

        -Frinny

        Comment

        • RobertTheProgrammer
          New Member
          • Aug 2007
          • 58

          #5
          Ah! I get it now. I just didn't understand the terminology.

          Many, many thanks for your assistance.

          Robert

          Comment

          Working...