c# - 如何禁用 GridView 控件内的此 LinkBut​​ton?

标签 c# asp.net linkbutton

我正在开发一个列出事件的注册系统,用户将能够在这些事件中注册。每个事件都有特定数量的座位。我现在面临的问题是,即使注册人数达到座位数,事件仍然有效,我无法通过禁用预订按钮来停止预订过程。

供您引用,我有以下数据库设计:

Event Table: ID, Title, NumberOfSeats
BookingDetails Table: BookingID, EventID, Username
User Table: Username, Name

事件将在 GridView 控件中列出,并且 GridView 内有 LinkBut​​ton 用于在事件中预订。我正在使用 ModalPopUp Extender 控件,这就是我使用 LinkBut​​ton 的原因,如下面的 ASP.NET 代码所示。在后面的代码中,在 GrivView_RowDataBound 内,我比较了每个事件的座位数和预订数。如果预订人数大于或等于座位数。应禁用预订按钮。我编写了代码,但我不知道为什么它不适合我,也不知道为什么我收到以下错误: 无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.LinkBut​​ton”类型。

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 &rarr;" 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进行进一步操作。

谢谢

关于c# - 如何禁用 GridView 控件内的此 LinkBut​​ton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029038/

相关文章:

c# - 使用 Microsoft.Practices.EnterpriseLibrary.Data IResultSetMapper<T>

c# - 从另一个用户应用程序在 Facebook 业务页面上发布

c# - 如何在gridviewc中获取按钮Id和字段名称

c# - 带参数的 LinkBut​​ton.OnClick 方法

asp.net - 动态生成的链接按钮不会触发 Onclick 事件 VB.Net

c# - 如何在gridview中显示计算值?

c# - SQLite 不存储/检索数据库

html - 切换到 HTML 设计 View 时 VS2008 部分卡住

asp.net - 浪费 Ajax 页面加载

ASP.NET GridView : Confirm button click postback with jQueryUI dialog