如何启用就地编辑在一个asp:GridView的? [英] How to enable in-place editing in an asp:GridView?

查看:206
本文介绍了如何启用就地编辑在一个asp:GridView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加编辑框,并阅读他们的价值观中提出,用 ASP:直放站


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

如何能启用 GridView控件的细胞可编辑的,例如(Photoshop的样机):


  

注意:我没有在Photoshop样机编辑框中到每一个行和列(因为它时间太长)。你还明白了吧。



  • 如何说服一个 ASP:GridView的来显示每个单元一个编辑框?

  • 如果我做说服 ASP:GridView的来显示一个编辑框,我如何读出他们的OnClick 保存按钮?

奖金颤振

我也不会反对使用 ASP:直放站,手动将< INPUT> 控制。那么我的困惑是如何在保存按钮的的OnClick 期间读取每个输入。虽然我将非常乐意使用一个中继器和一个GridView可能无法完成我想要做的中继唯一的可能性是什么,这个问题是关于一个GridView。


  • 如果在GridView的可以的做到这一点:伟大的;怎么样?

  • 如果在GridView的不能的做到这一点。这是一个答案太


解决方案

让你通过设置 DataGrid的 EditIndex 属性

例如:

 < ASP:GridView控件=服务器onrowediting =grdProducts_RowEditing
    ID =grdProducts>
    <柱体和GT;
        < ASP:CommandField中ShowEditButton =真/>
    < /专栏>
< / ASP:GridView的>

后面

code

 保护无效grdProducts_RowEditing(对象发件人,GridViewEditEventArgs E)
    {
        this.grdProducts.EditIndex = e.NewEditIndex;
        This.BindGrid();
    }

请注意,你必须重新绑定网格

通常你保存每行的数据,这意味着,你有一个编辑链接各行中,你进入编辑模式后,保存按钮和可选的取消按钮出现,让您保存在特定行的值

使用 GridView控件时,按照这一办法很简单:

 保护无效grdProducts_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
    {
        //当前行的旧值
        VAR oldValues​​ = e.OldValues​​;        //当前行的新(更新)值
        VAR newvalues​​ = e.NewValues​​;        //退出编辑模式
        this.grdProducts.EditIndex = -1;        //更新网格
        this.BindGrid();
    }

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

  onrowupdating =grdProducts_RowUpdating

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

 <柱体和GT;
        < ASP:的TemplateField的HeaderText =名称>
            <&EditItemTemplate的GT;
                < ASP:文本框ID =TextBox1的=服务器文本='<%#绑定(姓名)%>'>< / ASP:文本框>
            < / EditItemTemplate中>
            <&ItemTemplate中GT;
                < ASP:标签ID =Label1的=服务器文本='<%#绑定(姓名)%>'>< / ASP:标签>
            < / ItemTemplate中>
        < / ASP:的TemplateField>
      < /专栏>

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


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

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

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.

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

Bonus Chatter

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.

  • If the GridView can do it: great; how?
  • If the GridView cannot do it: that's an answer too.

解决方案

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

Example:

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

Code behind

    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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆