Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在asp:GridView中启用就地编辑?_Asp.net_Gridview_Inplace Editing - Fatal编程技术网

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

Asp.net 如何在asp:GridView中启用就地编辑?,asp.net,gridview,inplace-editing,Asp.net,Gridview,Inplace Editing,如何使用asp:Repeater,在提交过程中添加编辑框并读取其值 我有一个asp:GridView,它显示一组只读(即不可编辑)数据,例如: 如何使网格视图的单元格可编辑,例如(Photoshop实体模型): 注意:我没有在Photoshop中为每一行和每一列创建一个编辑框(因为它花费的时间太长)。你还是有这个想法 如何说服asp:GridView在每个单元格中显示编辑框 如果我确实说服了asp:GridView显示一个编辑框,我如何“读取”它们OnClickSave按钮的 花言巧语

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


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

如何使网格视图的单元格可编辑,例如(Photoshop实体模型):

注意:我没有在Photoshop中为每一行和每一列创建一个编辑框(因为它花费的时间太长)。你还是有这个想法

  • 如何说服
    asp:GridView
    在每个单元格中显示编辑框
  • 如果我确实说服了
    asp:GridView
    显示一个编辑框,我如何“读取”它们
    OnClickSave按钮的
花言巧语 我不反对使用
asp:Repeater
,手动放置
控件。然后,我的困惑是如何在单击保存按钮的
OnClick
时读取每个输入。虽然我非常乐意使用转发器,而GridView可能无法实现我想要的,使转发器成为唯一的可能,但这个问题是关于GridView的

  • 如果GridView可以做到:太好了;怎么做
  • 如果GridView不能做到这一点:这也是一个答案

您是否尝试设置
数据网格的
编辑索引
属性

例如:

<asp:GridView runat="server" onrowediting="grdProducts_RowEditing" 
    ID="grdProducts">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>
请注意,必须重新绑定网格

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

在使用
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();
    }
在网格标记中,只需添加以下内容:

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

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

您可以使用GridView执行此操作,但如果有许多列,您将生成大量代码

首先生成GridView TemplateFields中的所有列。在我的示例中,我将只使用一列。像这样:

<asp:GridView ID="test" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Foo" SortExpression="foo">
            <ItemTemplate>
                <asp:TextBox ID="FooText" runat="server" Text='<%# Eval("Foo") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <!-- other cols here -->
    </Columns>
</asp:GridView>
<asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Click" />

然后,在“保存”按钮的代码隐藏中,您可以在GridView中的行上进行迭代:

foreach (GridViewRow gvRow in test.Rows)
{
    string val = ((TextBox)gvRow.FindControl("FooText")).Text;
    <!-- other cols here -->
    //do something with all the values you have parsed from the row
}
foreach(test.Rows中的GridViewRow gvRow)
{
字符串val=((文本框)gvRow.FindControl(“FooText”).Text;
//对从该行解析的所有值执行某些操作
}

整个列都应该是可编辑的,对吗?不只是一排吗?@Blachshma是的。用户可能希望/需要编辑所有项目。
RowEditing
事件的问题是,您只能编辑每行数据,一次只能编辑一行,并且必须有一个编辑按钮命令链接,用户必须先单击该链接。
This.BindGrid()
?只是一个绑定网格的自定义函数,如果在
ItemTemplate
中使用
Bind
而不是
Eval
,则通常会设置网格数据源属性的函数,它应该自动更新基础数据值吗?
foreach (GridViewRow gvRow in test.Rows)
{
    string val = ((TextBox)gvRow.FindControl("FooText")).Text;
    <!-- other cols here -->
    //do something with all the values you have parsed from the row
}