title: How to disable this LinkButton inside GridView control?
body: <p>I am developing a registration system that lists events and the users will be able to register in these events. Each event has a specific number of seats. The problem that I am facing now is even when the number of registration reaches the number of seats, the event is still avaiable and I cannot stop the booking process by disabling booking button.</p>
  
  <p>For your information, I have the following database design:</p>
  
  <pre><code>Event Table: ID, Title, NumberOfSeats
  BookingDetails Table: BookingID, EventID, Username
  User Table: Username, Name
  </code></pre>
  
  <p>The events will be listed in a GridView control and there is LinkButton inside the GridView for booking in the event. I am using a ModalPopUp Extender control and this is why I am using a LinkButton as shown in the ASP.NET code below. In the code-behind, inside the GrivView_RowDataBound, I compared between the number of seats and the number of bookings for each event. If the number of bookings greater than or equal to the number of seats. Booking button should be disabled. I wrote the code but I don't know why it is not working with me and why I am getting the following error:
  <strong><em>Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.</em></strong> </p>
  
  <p><em><strong>ASP.NET code:</em></strong></p>
  
  <pre><code>&lt;asp:GridView ID="ListOfAvailableEvents_GrivView" runat="server" AutoGenerateColumns="False"
                      DataKeyNames="ID" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
                      GridLines="None" AllowPaging="True" PageSize="10" 
                      onrowdatabound="ListOfAvailableEvents_GrivView_RowDataBound"&gt;
                      &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
                      &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" /&gt;
                      &lt;Columns&gt;
                          &lt;asp:TemplateField HeaderText=""&gt;
                              &lt;ItemTemplate&gt;
                                  &lt;asp:LinkButton ID="lnkTitle" runat="server" CssClass="button" Text="Book &amp;rarr;" OnClick="lnkTitle_Click"&gt;&lt;/asp:LinkButton&gt;
                              &lt;/ItemTemplate&gt;
                          &lt;/asp:TemplateField&gt;
                          &lt;asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /&gt;
                          &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt;
                          &lt;asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /&gt;
                          &lt;asp:BoundField DataField="StartDateTime" HeaderText="Start Date &amp; Time" SortExpression="StartDateTime" /&gt;
                          &lt;asp:BoundField DataField="EndDateTime" HeaderText="End Date &amp; Time" SortExpression="EndDateTime" /&gt;
                      &lt;/Columns&gt;
                      &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
                      &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
                      &lt;HeaderStyle Font-Bold="True" CssClass="complete" /&gt;
                      &lt;EditRowStyle BackColor="#999999" /&gt;
                      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
                      &lt;EmptyDataTemplate&gt;&lt;h2&gt;No Events Available&lt;/h2&gt;&lt;/EmptyDataTemplate&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><em><strong>Code-Behind (C#) code:</em></strong></p>
  
  <pre><code>protected void ListOfAvailableEvents_GrivView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          int numberOfBookings = 0;
          int numberOfAvailableSeats = 0;
  
          string connString = "..........."
          string selectCommand = @"SELECT     COUNT(*) AS UserBookingsCount, dbo.Events.NumberOfSeats
                                      FROM         dbo.BookingDetails INNER JOIN
                                                            dbo.Events ON dbo.BookingDetails.EventID = dbo.Events.ID
                                      GROUP BY dbo.Events.NumberOfSeats";
          using (SqlConnection conn = new SqlConnection(connString))
          {
              //Open DB Connection
              conn.Open();
              using (SqlCommand cmd = new SqlCommand(selectCommand, conn))
              {
                  SqlDataReader reader = cmd.ExecuteReader();
                  if (reader != null)
                      if (reader.Read())
                      {
                          numberOfBookings = Int32.Parse(reader["UserBookingsCount"].ToString());
                          numberOfAvailableSeats = Int32.Parse(reader["NumberOfSeats"].ToString());
                      }
              }
              //Close the connection
              conn.Close();
          }
  
          if (numberOfBookings &gt;= numberOfAvailableSeats)
          {
              LinkButton bookButton = (LinkButton)(sender);
              bookButton.Enabled = false;
          }
      }
  </code></pre>
  
  <p><strong>So could you please tell me how to fix this problem?</strong></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The GridView lists many different events. Let us take one of them. If event A has 3 available seats and the number of bookings reaches 3, the 'Book' button should be disabled only for this event not for all of them. So the button will be disabled for this event when the number of bookings reaches the number of available seats.</p>