Asp.net “如何禁用”;编辑";Gridview中的按钮?

Asp.net “如何禁用”;编辑";Gridview中的按钮?,asp.net,gridview,edititemtemplate,Asp.net,Gridview,Edititemtemplate,Iam使用“我的网格视图”中的“项目模板”字段更新特定列中的值。 ItemTemplate字段包含“label”控件,EditItemTemplate包含“DropDownList”。现在的问题是,我需要根据“标签”的值禁用“编辑”按钮。。。附上编码行。谁能给我一个解决办法 Home.Aspx: ********** <Columns> <asp:BoundField DataField="Date" HeaderText="Da

Iam使用“我的网格视图”中的“项目模板”字段更新特定列中的值。 ItemTemplate字段包含“label”控件,EditItemTemplate包含“DropDownList”。现在的问题是,我需要根据“标签”的值禁用“编辑”按钮。。。附上编码行。谁能给我一个解决办法

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>
Home.Aspx:
**********

在我的编码中,“lblName”在ItemTemplate中具有状态值,“ddlState”在EditItemTemplate中具有状态值。根据“lblName”值,必须启用“编辑”选项

将编辑
命令字段
转换为
模板字段

在新生成的
按钮中添加以下标记:

Enabled=''

在代码隐藏中,创建一个新方法:

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

让我知道这对您的作用。

将编辑的
命令字段
转换为
模板字段

在新生成的
按钮中添加以下标记:

Enabled=''

在代码隐藏中,创建一个新方法:

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

让我知道这对您是如何工作的。

另一种方法是使用RowDataBound,这样您就可以直接使用Status的值。这假定您正在为数据源使用DataTable或其他DataRow集合。如果使用不同的数据类型,则需要更新数据项转换

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

另一种方法是使用RowDataBound,以便可以直接使用Status的值。这假定您正在为数据源使用DataTable或其他DataRow集合。如果使用不同的数据类型,则需要更新数据项转换

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

谢谢@Chris Porter,这很有效。但我不想让ddlstate显示为false,我需要禁用命令字段中的编辑按钮…@ognale88,很抱歉出现了下拉错误。我已经更新了我的示例,以显示编辑按钮而不是下拉菜单。Thankx@Chris Porter,它可以工作。但我不想让ddlstate显示为false,我需要禁用命令字段中的编辑按钮…@ognale88,很抱歉出现了下拉错误。我已经更新了我的示例,以显示编辑按钮而不是下拉列表。