问题描述
如何使用 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 中为每一行和每一列制作一个编辑框(因为它花费了太长时间).你还是明白的.
- 如何说服
asp:GridView
在每个单元格中显示一个编辑框? - 如果我确实说服
asp:GridView
显示一个编辑框,我如何读取"它们OnClick
的 Save 按钮? - 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" themOnClick
of Save button? - 如果 GridView 可以做到:太好了;怎么样?
- 如果 GridView 不能做到:那也是一个答案.
- If the GridView can do it: great; how?
- If the GridView cannot do it: that's an answer too.
我不反对使用 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.
推荐答案
您是否尝试过设置 DataGrid
的 EditIndex
属性?
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 中启用就地编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!