禁用gridview中特定用户的按钮字段

时间:2014-04-20 16:42:37

标签: c# asp.net gridview

这可能会让人感到有些困惑,但我会尽力解释并感谢任何帮助。我有一个类似的网格视图

<asp:GridView ID="GridView1" runat="server" Width="936px" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" style="text-align: center" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="TaskId" HeaderText="TaskId" InsertVisible="False" ReadOnly="True" SortExpression="TaskId" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Body" HeaderText="Body" SortExpression="Body" Visible="false" />
        <asp:BoundField DataField="Reward" HeaderText="Reward(Rs)" SortExpression="Reward" />
        <asp:BoundField DataField="TimeAllotted" HeaderText="Time(Min)" SortExpression="TimeAllotted" />
        <asp:BoundField DataField="PosterName" HeaderText="Uploader" SortExpression="PosterName" />
        <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Perform Task" ControlStyle-ForeColor="White"  ControlStyle-Font-Bold="true">
            <ControlStyle BackColor="#CC6600" Font-Bold="True" ForeColor="White"></ControlStyle>
        </asp:ButtonField>
    </Columns>

当我点击特定行的按钮字段时,我会被定向到一个新页面,要求我执行某项任务。 我想在执行任务时禁用该特定用户的“执行任务”按钮字段。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ...>
    Other settings
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        var button = (Button) e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
        button.Enabled = CanCurrentUserViewButton();
    }
}

private bool CanCurrentUserViewButton()
{
    //Logic...
}
相关问题