0

This might get a little confusing but I will try my best to explain and would appreciate any help.I have a gridview like

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

When I click on the buttonfield of a particular row, I get directed to a new page which asks me to perform some task. I want to disable the 'perform task' buttonfield for that particular user whenever a task is performed.

How can I do that?

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Mash
  • 167
  • 1
  • 7
  • 25
  • create column which maintain status of your tasks in database TaskStatus on this you can enable or disable your buttons – Dgan Apr 20 '14 at 16:50
  • but how would i manage it for individual user logins?? – Mash Apr 20 '14 at 16:54
  • You need to disable the `Perform Task` button if `PosterName` refers the current user, right? Or to enable/disable `PerformTask` column at all, depending on the current user identity? – nativehr Apr 20 '14 at 21:53
  • @nativehr - I want to disable the PerformTask buttonfield for a particular user.not all the button but only the one's which he has clicked. – Mash Apr 21 '14 at 07:22
  • You can write some code-behind to decide if user should view or not the button. Check out this post please: http://stackoverflow.com/questions/1461302/conditionally-hide-commandfield-or-buttonfield-in-gridview – nativehr Apr 21 '14 at 07:37
  • @nativehr I have a code written for the buttonfield,i cannot change it to button.So what else can i do to hide the buttonfield on some ondition? – Mash Apr 21 '14 at 08:07

1 Answers1

1

Try this code:

<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...
}
nativehr
  • 1,131
  • 6
  • 16