如何在 asp:GridView 中启用就地编辑?

编程入门 行业动态 更新时间:2024-10-22 03:04:19
本文介绍了如何在 asp:GridView 中启用就地编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何使用 asp:Repeater 添加编辑框,并在提交过程中读取它们的值?

How can i add edit boxes, and read their values during submit, with an asp:Repeater?

我有一个 asp:GridView 显示只读(即不可编辑)数据集,例如:

i have an asp:GridView which is displaying a read-only (i.e. non-editable) set of data, e.g.:

如何使 GridView 的单元格可编辑,例如(Photoshop Mockup):

How can i enabled the cells of the GridView to be editable, e.g (Photoshop Mockup):

注意:我没有在 Photoshop 中为每一行和每一列制作一个编辑框(因为它花费了太长时间).你还是明白的.

Note: i didn't mockup in Photoshop an edit box into every row and column (cause it was taking too long). You still get the idea.

如何说服 asp:GridView 在每个单元格中显示一个编辑框?如果我确实说服 asp:GridView 显示一个编辑框,我如何读取"它们 OnClickSave 按钮?

How can i convince an asp:GridView to show an edit-box in each cell? If i do convince the asp:GridView to show an edit-box, how do i "read" them OnClick of Save button?

我不反对使用 asp:Repeater,手动放置 控件.我的困惑是如何在 Save 按钮的 OnClick 期间读取每个输入.虽然我非常乐意使用中继器,并且 GridView 可能无法完成我想要的使中继器成为唯一可能的事情,但这个问题是关于 GridView.

i would not be opposed to using an asp:Repeater, manually placing <INPUT> controls. My confusion then is about how to read each input during the OnClick of the Save button. And although i would be perfectly happy to use a repeater, and a GridView might not be able to accomplish what i want making the repeater the only possibility, this question is about a GridView.

如果 GridView 可以做到:太好了;怎么样?如果 GridView 不能做到:那也是一个答案. If the GridView can do it: great; how? If the GridView cannot do it: that's an answer too.

推荐答案

您是否尝试过设置 DataGridEditIndex 属性?

Have you tried by setting up the EditIndex property of the DataGrid?

示例:

<asp:GridView runat="server" onrowediting="grdProducts_RowEditing" 
    ID="grdProducts">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

背后的代码

    protected void grdProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.grdProducts.EditIndex = e.NewEditIndex;
        This.BindGrid();
    }

注意你必须重新绑定你的网格

Note that you have to re-bind your grid

通常每行保存数据,这意味着每行都有一个编辑链接,进入编辑模式后,会出现一个保存按钮和可选的取消按钮,这将允许您保存该特定行的值

Usually you save the data per row, which means, you have an edit link in each row and after you enter edit mode, a save button and optionally a cancel button appear which will allow you to save the values of that specific row

在使用 GridView 时遵循这种方法是微不足道的:

Following this approach is trivial when using the GridView:

    protected void grdProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // old values for the current row
        var oldValues = e.OldValues;

        // new (updated) values for the current row
        var newvalues = e.NewValues;

        // Exit edit mode
        this.grdProducts.EditIndex = -1;

        // Update the grid
        this.BindGrid();
    }

在网格标记中添加以下内容:

In the grid markup just add the following:

    onrowupdating="grdProducts_RowUpdating"

如果您在编辑或以只读模式显示单元格数据时需要指定自定义控件,请使用网格模板:

If you need to specify custom controls when editing or when displaying the cell data in read-only mode, use grid templates:

       <Columns>
        <asp:TemplateField HeaderText="Name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
      </Columns>

这篇关于如何在 asp:GridView 中启用就地编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:26:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   如何在   asp   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!