我正在开发一个列出事件的注册系统,用户将能够在这些事件中注册。每个活动都有特定数量的座位。我现在面临的问题是即使注册数达到座位数,事件仍然可用,我无法通过禁用预订按钮来停止预订过程。
为了您的信息,我有以下数据库设计:
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,则应仅为此活动禁用“预订”按钮,而不是所有活动。因此,当预订数量达到可用座位数时,该事件将被禁用。
答案 0 :(得分:1)
尝试:
LinkButtton lbtn = new LinkButtton();
lbtn = (LinkButton)e.Row.FindControl("ButtonId");
然后使用lbtn
进行进一步操作。
由于
答案 1 :(得分:1)
请使用以下代码隐藏LinkButton,即lnkTitle
。
代码如下:
protected void ListOfAvailableEvents_GrivView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int numberOfBookings = 0;
int numberOfAvailableSeats = 0;
string connString = "Data Source=appServer\\sqlexpress;Initial Catalog=EventRegMgnSysDB;Integrated Security=True;";
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 (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkTitle = (LinkButton )e.Row.FindControl("lnkTitle");
if (numberOfBookings >= numberOfAvailableSeats)
{
lnkTitle.Visible = false;
}
else
{
lnkTitle.Visible = true;
}
}
}
如果您能得到答案,请注明。
答案 2 :(得分:0)
RowDataBound事件用于GridView,因此无法将sender参数强制转换为LinkButton。
修改代码以包含以下内容。
if(numberOfBookings >= numberOfAvailableSeats)
{
if (e.Row.Cells[0].HasControls())
{
var button = e.Row.Cells[0].Controls[1] as LinkButton;
button.Enabled = false;
}
}
我担心你如何接近这个。
RowDataBound事件触发它所绑定的数据源中的每一行,并且每次都在进行数据库查询。
您可以尝试在初始查询中包含numberOfBookings和numberOfAvailableSeats,这些将可供您使用,您可以每次(每行)检查它们,而无需每次都访问数据库。