Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在gridview中为特定用户禁用按钮字段_C#_Asp.net_Gridview - Fatal编程技术网

C# 在gridview中为特定用户禁用按钮字段

C# 在gridview中为特定用户禁用按钮字段,c#,asp.net,gridview,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" BackCo

这可能会让人有点困惑,但我会尽力解释,并感谢任何帮助

<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>

当我单击某一行的按钮字段时,我会被引导到一个新页面,该页面要求我执行一些任务。 每当执行任务时,我想为该特定用户禁用“执行任务”按钮字段

我如何才能做到这一点?

请尝试以下代码:

<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...
}

其他设置
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var button=(button)e.Row.Cells[e.Row.Cells.Count-1]。控件[0];
button.Enabled=CanCurrentUserViewButton();
}
}
私有布尔值CanCurrentUserViewButton()
{
//逻辑。。。
}

在数据库TaskStatus中创建一列,用于维护任务的状态。在该列上,您可以启用或禁用按钮,但我如何管理单个用户登录?如果
PosterName
引用当前用户,您需要禁用
执行任务
按钮,对吗?或者,根据当前用户身份,是否启用/禁用
PerformTask
列?@nativehr-我想禁用特定用户的PerformTask按钮字段。不是所有按钮,而是他单击的按钮。您可以编写一些代码来决定用户是否应查看按钮。请查看此帖子: