0

Iam using Item Template field in my gridview to update the values inside particular column. The ItemTemplate field contains "label" control and EditItemTemplate contains "DropDownList". Now the problem is I need to disable the "Edit" button based on the value of "Label"... Attached the lines of coding. Can anyone give me a solution.

Home.Aspx:
**********
   <Columns>

                    <asp:BoundField DataField="Date" HeaderText="Date" ReadOnly="true" />
                    <asp:BoundField DataField="Type" HeaderText="Type" ReadOnly="true" />
                    <asp:BoundField DataField="Reason" HeaderText="Reason" ReadOnly="true" />
                    <asp:BoundField DataField="Request By" HeaderText="Request By" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="center">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlState" AutoPostBack="false" runat="server">
                                <asp:ListItem Text="Approved" Value="Approved">  </asp:ListItem>
                                <asp:ListItem Text="Declined" Value="Declined">  </asp:ListItem>
                                <asp:ListItem Text="Pending" Value="Pending">  </asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" />
                </Columns>

Here in my coding "lblName" has the status value in ItemTemplate and "ddlState" has the status value in EditItemTemplate. Based upon the "lblName" value , "Edit" option has to be enabled...

ognale88
  • 169
  • 3
  • 7
  • 19

2 Answers2

3

Another approach is to use RowDataBound so you can use the value of Status directly. This assumes you are using a DataTable or other DataRow collection for your data source. The DataItem cast will need to be updated if you are using a different data type.

<asp:GridView ID="ExampleGridView" runat="server" OnRowDataBound="ExampleGridView_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="center">
            <EditItemTemplate>
                <asp:DropDownList ID="StateDropDownList" AutoPostBack="false" runat="server">
                    <asp:ListItem Text="Approved" Value="Approved" />
                    <asp:ListItem Text="Declined" Value="Declined" />
                    <asp:ListItem Text="Pending" Value="Pending" />
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Status") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" Visible="true" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

From code behind you can handle the row independently and have access to most of what is going on:

protected void ExampleGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow 
        && (
            e.Row.RowState == DataControlRowState.Alternate
            || e.Row.RowState == DataControlRowState.Normal
            || e.Row.RowState == DataControlRowState.Selected
        ))
    {
        Button EditButton = (Button)e.Row.FindControl("EditButton");
        System.Data.DataRow dataRecord = (System.Data.DataRow)e.Row.DataItem;
        if (EditButton != null && dataRecord != null)
        {
            if (dataRecord["Status"] == "ValueThatShowsEditButton")
            {
                EditButton.Visible = true;
            }
        }
    }
}
Chris Porter
  • 3,627
  • 25
  • 28
  • Thankx @Chris Porter, it works. But I dont want to make the ddlstate visible as false, I need to make the Edit Button in command field to be Disabled.... – ognale88 Aug 06 '13 at 06:03
  • @ognale88, sorry for the dropdown mistake. I've updated my example to show the edit button instead of the dropdown. – Chris Porter Aug 07 '13 at 16:07
2

Convert your Edit CommandField to a TemplateField.

In the newly-generated Button, add the following markup:

Enabled='<%# IsEditEnabled(Eval("Status")) %>'

In your code-behind, create a new method:

protected bool IsEditEnabled(string statusValue)
{
    // Here is where you determine the value
}

Let me know how that works for you.

Garrison Neely
  • 3,238
  • 3
  • 27
  • 39