user2377796
user2377796

Reputation:

ASP Gridview - Column that either displays button or text

I have a Gridview that works like a timetable. There are 3 Columns in the gridview.

Columns are as below

  1. From

  2. Till

  3. Reservate

I want to have a button on the reservate column as long as the spot is free. If the spot is taken I want to display some text instead. Currently I am using below markup

<asp:TemplateField HeaderText="Kranbeladung - Crane loading">
    <ItemTemplate>
       <asp:Button ID="rBtn" runat="server" Text="Reservate" />
    </ItemTemplate>
</asp:TemplateField>

Any idea how I can achieve that? Thanks

Upvotes: 1

Views: 131

Answers (2)

Matteo Maestri
Matteo Maestri

Reputation: 61

Greg answer is great but I had to do some changes. With his code I got this in my page:

   System.Web.UI.WebControls.Button

instead of the Control itself.

In order to work I returned the HTML of the Cotrol, like that:

    protected string CheckAvailability(object obj)
    {
        //some logic
        return "<asp:Button runat='server'>ButtonText</asp:Button>";
    }

Upvotes: 1

Greg
Greg

Reputation: 11480

Since you have a trigger, you would have a method that would test the column. Then populate according to the result. Below is what you would have in your Grid.

<asp:TemplateField HeaderText="...">
     <ItemTemplate>
          <%# CheckAvailability(Eval("Column")) %>
     </ItemTemplate>
</asp:TemplateField>

Then you would do the following in code behind:

protected Control CheckAvailability(object flag)
{
     if(flag != null)
     {
          // Create a button, then return it.
     }

     else 
     {
          // Create a label, say space available and return.
     }
}

Upvotes: 0

Related Questions