我正在开发一个列出事件的注册系统,用户将能够在这些事件中注册。每个活动都有特定数量的座位。我现在面临的问题是,即使注册人数达到座位数,活动仍然可用,我无法通过禁用预订按钮来停止预订过程。
供您参考,我有以下数据库设计:
Event Table: ID, Title, NumberOfSeats
BookingDetails Table: BookingID, EventID, Username
User Table: Username, Name
事件将在 GridView 控件中列出,并且 GridView 内有 LinkButton 用于在事件中进行预订。我正在使用 ModalPopUp Extender 控件,这就是我使用 LinkButton 的原因,如下面的 ASP.NET 代码所示。在代码隐藏中,在 GrivView_RowDataBound 内,我比较了每个活动的座位数和预订数。如果预订人数大于或等于座位数。预订按钮应该被禁用。我编写了代码,但我不知道为什么它不适合我以及为什么我收到以下错误: 无法将类型为“System.Web.UI.WebControls.GridView”的对象转换为类型“System.Web. UI.WebControls.LinkButton'。
ASP.NET 代码:
<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">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkTitle" runat="server" CssClass="button" Text="Book →" OnClick="lnkTitle_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="StartDateTime" HeaderText="Start Date & Time" SortExpression="StartDateTime" />
<asp:BoundField DataField="EndDateTime" HeaderText="End Date & Time" SortExpression="EndDateTime" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle Font-Bold="True" CssClass="complete" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EmptyDataTemplate><h2>No Events Available</h2></EmptyDataTemplate>
</asp:GridView>
代码隐藏 (C#) 代码:
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 >= numberOfAvailableSeats)
{
LinkButton bookButton = (LinkButton)(sender);
bookButton.Enabled = false;
}
}
那么你能告诉我如何解决这个问题吗?
更新:
GridView 列出了许多不同的事件。让我们采取其中之一。如果活动 A 有 3 个可用座位且预订数量达到 3,则应仅针对此活动禁用“预订”按钮,而不是针对所有活动。因此,当预订数量达到可用座位数时,此事件的按钮将被禁用。