我有一个Gridview,删除和编辑按钮看起来像
<asp:GridView ID="grdTrackedItems" runat="server" AutoGenerateColumns="False" Width="330px"
BorderStyle="None" OnRowDataBound="OnRowDataBoundTrackedItems" OnRowDeleting="OnRowDeletingTrackedItems">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%# Eval("Item") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/Resources/Images/Delete.png"
Height="12" Width="12" ToolTip="Delete" CommandName="Delete" />
</ItemTemplate>
<ItemStyle Width="22px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/Resources/Images/Edit.png"
Height="12" Width="12" ToolTip="Edit" />
</ItemTemplate>
<ItemStyle Width="22px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我在页面底部有一个文本框和“保存”按钮。我想在gridview中单击编辑按钮时在文本框中显示项目名称。我怎么能这样做?
答案 0 :(得分:3)
在编辑按钮单击处理程序中,您可以访问当前行,如下所示:
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewRow row = ((ImageButton)sender).Parent.Parent as GridViewRow;
// Do some thing with this row
}
答案 1 :(得分:3)
为RowEditing事件提供处理程序,然后在该处理程序中更新文本框。
<asp:GridView ID="grdTrackedItems" runat="server"
AutoGenerateColumns="False" Width="330px"
...
OnRowEditing="EditRecord">
<Columns>
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
Label lbl = (Label)grdTrackedItems.Rows[e.NewEditIndex].Cells[0].FindControl("lblItem");
MyTextBox.Text = lbl.text;
}
答案 2 :(得分:2)
我从river-blue's回答了解决方案。
将DataKeyNames =“Item”添加到Gridview 并在编辑按钮内单击处理程序
protected void OnClickEdit( object sender, EventArgs e )
{
GridViewRow row = ( (ImageButton) sender ).Parent.Parent as GridViewRow;
txtName.Text = grdTrackedItems.DataKeys[row.RowIndex].Value.ToString();
}