如何在GridView控件中禁用LinkButton
本文关键字:LinkButton 控件 GridView | 更新日期: 2023-09-27 17:49:24
我正在开发一个注册系统,列出事件和用户将能够在这些事件中注册。每个活动都有特定数量的座位。我现在面临的问题是,即使注册人数达到座位数,活动仍然可用,我无法通过禁用预订按钮来停止预订过程。
供您参考,我有以下数据库设计:
Event Table: ID, Title, NumberOfSeats
BookingDetails Table: BookingID, EventID, Username
User Table: Username, Name
事件将在GridView控件中列出,并且在GridView中有LinkButton用于预订事件。我正在使用ModalPopUp扩展器控件,这就是为什么我使用链接按钮,如在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,则"预订"按钮应仅在该活动中禁用,而不是在所有活动中禁用。因此,当预订数量达到可用座位数量时,此按钮将被禁用。
尝试:
LinkButtton lbtn = new LinkButtton();
lbtn = (LinkButton)e.Row.FindControl("ButtonId");
则使用lbtn
进行下一步操作。
谢谢
请使用下面的代码隐藏链接按钮,即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;
}
}
}
如果你想得到答案,请在上面做个记号。 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事件为绑定到的数据源中的每一行触发,并且每次都执行数据库查询。
您可以尝试在初始查询中包含numberofbooking和numberOfAvailableSeats,这些将对您可用,您可以每次(每行)检查它们,而不必每次都去数据库