使“数据绑定网格”视图中的“单个”字段可编辑

本文关键字:字段 单个 编辑 视图 数据绑定 网格 | 更新日期: 2023-09-27 17:58:44

我希望使MAC列可编辑,因为这是一个注册应用程序,这将允许用户更新他们的注册。我已经绑定了来自SQL数据库的数据,这很好。我只是不知道如何绑定当前的MAC,并且仍然可以编辑。任何帮助都将不胜感激,我已经在下面发布了我的asp.net(4.5的目标框架)代码。

<asp:GridView ID="DataDisplay" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" Height="78px" Width="727px" ViewStateMode="Enabled"
        OnRowCancelingEdit="DataDisplay_RowCancelingEdit" OnRowDeleting="DataDisplay_RowDeleting"
        OnRowEditing="DataDisplay_RowEditing" style="margin-right: 0px">
        <Columns>
            <asp:BoundField HeaderText="Device" DataField="Device" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField HeaderText="MAC Address" DataField="MAC Address" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField HeaderText="Area" DataField="Area" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField HeaderText="Date Registered" DataField="Date Registered" ReadOnly="true" ItemStyle-HorizontalAlign="Center"/>
            <asp:CommandField ShowEditButton="true" />
            <asp:CommandField ShowDeleteButton="true" />
        </Columns>
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />
    </asp:GridView>

使“数据绑定网格”视图中的“单个”字段可编辑

将"MAC地址"BoundField替换为模板字段:

<asp:TemplateField HeaderText="MAC Address" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblMacAddress" runat="server" Text='<%#Eval("MAC Address") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtMacAddress" Text='<%#Bind("[Mac Address]") %>' runat="server"></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

编辑:

其余的字段应该只有ItemTemplate,比如这个:

<asp:TemplateField HeaderText="Device" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblDevice" runat="server" Text='<%#Eval("Device") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

BTW-数据字段名称中的空格不是个好主意!