gpt4 book ai didi

c# - 当 BoundField 为零时禁用 ButtonField

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:47 25 4
gpt4 key购买 nike

我有这个 GridView,它填充了 Page_Load:

protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
GridView1.DataSource = actBO.BuscarActividades();
GridView1.DataBind();
}
}

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Class" HeaderText="Class" />
<asp:BoundField DataField="Day" HeaderText="Day" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="Vacants" HeaderText="Vacants" />

<asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/>

</Columns>
</asp:GridView>

其中“Vacants”列显示一个 int(它表示一个类(class)中的空置预订空间数量)。

每一行都有一个用于预订特定类(class)的按钮。我需要为“空缺”字段何时为零设置条件,以便禁用“预订”按钮。

到目前为止,它是这样的:image .

如您所见,我需要在没有更多空缺时禁用该按钮。它应该无法被点击。

最佳答案

为此,您必须注册 OnRowDataBound 事件。更多解释可以在here中找到.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Class" HeaderText="Class" />
<asp:BoundField DataField="Day" HeaderText="Day" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="Vacants" HeaderText="Vacants" />
<asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/>
</Columns>
</asp:GridView>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get your button via the column index; ideally you could use template field and put your own button inside
var button = e.Row.Cell[5].Controls[0] as Button;
int vacant = 0;
var vacantVal = int.TryParse(e.Row.Cell[4].Text, out vacant);
if (button != null)
{
button.Enabled = vacant > 0;
}
}
}

希望对您有所帮助。

关于c# - 当 BoundField 为零时禁用 ButtonField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40385098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com