0

我在我的网格视图中使用项目模板字段来更新特定列中的值。ItemTemplate 字段包含“标签”控件,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>

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

4

2 回答 2

2

将您的编辑转换CommandFieldTemplateField.

在新生成的Button中,添加以下标记:

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

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

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

让我知道这对你有用。

于 2013-08-05T16:08:37.420 回答
2

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

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

从后面的代码中,您可以独立处理该行并可以访问大部分正在发生的事情:

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;
            }
        }
    }
}
于 2013-08-05T16:21:23.497 回答