You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2019中ASP GridView基于Approved状态动态设置Goal单元格可编辑(VB)

解决方案

要实现当Approved字段为"No"时,编辑状态下的Goal字段可编辑,需要通过替换BoundField为TemplateField并动态控制编辑控件的只读状态来实现,具体步骤如下:

1. 修改GridView的Goal字段为TemplateField

将原有的BoundField替换为TemplateField,自定义编辑模板中的控件,方便动态控制:

<asp:GridView ID="grdgoals" runat="server" AutoGenerateColumns="False" DataSourceID="DS1" Height="225px" Width="1001px" BorderColor="#003960" BorderStyle="Solid" BorderWidth="1px" DataKeyNames="goalid" EmptyDataText="No goals found." Font-Bold="True" Font-Names="Calibri" Font-Overline="False" Font-Size="Medium" Font-Strikeout="False" ForeColor="#00AD86" ShowHeaderWhenEmpty="True" AllowSorting="True" style="margin-right: 21px" OnRowDataBound="grdgoals_RowDataBound">
    <Columns>
        <asp:BoundField DataField="goalid" HeaderText="goalid" ReadOnly="True" SortExpression="goalid" Visible="False" />
        <asp:CommandField EditText="EDIT" ShowEditButton="True" ShowHeader="True">
            <HeaderStyle BorderColor="#003960" />
        <ItemStyle Font-Names="Calibri" Font-Underline="True" ForeColor="#006EAA" HorizontalAlign="Center" BorderColor="#003960" Width="20px" />
        </asp:CommandField>
        <!-- 替换原BoundField为TemplateField -->
        <asp:TemplateField HeaderText="Goal" SortExpression="goaltext">
            <HeaderStyle BorderColor="#003960" />
            <ItemStyle BorderColor="#003960" Width="250px" />
            <ItemTemplate>
                <%# Eval("goaltext") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtGoal" runat="server" Text='<%# Bind("goaltext") %>' Width="240px"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ConvertEmptyStringToNull="True" DataField="type" HeaderText="Type" ReadOnly="True" >
        <ItemStyle Width="70px" />
        </asp:BoundField>
        <asp:BoundField DataField="progress" HeaderText="Progress %" SortExpression="progress" >
            <HeaderStyle BorderColor="#003960" />
        <ItemStyle HorizontalAlign="Center" BorderColor="#003960" VerticalAlign="Middle" Width="20px" />
        </asp:BoundField>
        <asp:BoundField DataField="comments" HeaderText="Comments" SortExpression="comments" ItemStyle-Wrap="true">
            <ControlStyle Height="400px" Width="240px" />
            <HeaderStyle BorderColor="#003960" />
            <ItemStyle BorderColor="#003960" Width="250px" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="approved" HeaderText="Approved" ReadOnly="True" SortExpression="approved" >
        <ItemStyle HorizontalAlign="Center" Width="40px" />
        </asp:BoundField>
    </Columns>
    <EditRowStyle HorizontalAlign="Left" VerticalAlign="Top" />
    <HeaderStyle ForeColor="#006EAA" />
</asp:GridView>

2. 编写后台VB代码动态控制编辑状态

在页面后台代码中,处理GridView的RowDataBound事件,根据当前行的Approved值设置Goal字段编辑控件的只读状态:

Protected Sub grdgoals_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    '仅处理编辑状态的行
    If e.Row.RowState = DataControlRowState.Edit OrElse e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Alternate) Then
        '获取当前行的Approved字段值
        Dim approvedValue As String = DataBinder.Eval(e.Row.DataItem, "approved").ToString().Trim()
        
        '找到编辑模板中的TextBox控件
        Dim txtGoal As TextBox = DirectCast(e.Row.FindControl("txtGoal"), TextBox)
        If txtGoal IsNot Nothing Then
            '当Approved为"No"时允许编辑,否则设为只读
            txtGoal.ReadOnly = (approvedValue <> "No")
            
            '可选:通过样式区分状态
            txtGoal.BackColor = If(approvedValue = "No", System.Drawing.Color.White, System.Drawing.Color.LightGray)
        End If
    End If
End Sub

说明

  • 替换BoundField为TemplateField是因为原BoundField的ReadOnly属性是全局静态的,无法根据每行数据动态调整;
  • RowDataBound事件会在每行数据绑定后触发,此时可以准确获取当前行的Approved值,并控制编辑控件的状态;
  • 注意判断Approved值的大小写,如果数据库中存储的是小写"no",需要将判断条件改为approvedValue <> "no"。

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 16:00:59