views:

488

answers:

3

I have a Gridview with delete and edit buttons looks like

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

I have a text box and Save button on bottom of the page. I want to display the item name in the textbox when I click on the edit button from gridview. How can I do this?

+1  A: 

inside the edit button click handler you can access the current row as below:

protected void Button1_Click1(object sender, EventArgs e)
{
    GridViewRow row = ((ImageButton)sender).Parent.Parent as GridViewRow;
    // Do some thing with this row
}
+3  A: 

Provide a handler for the RowEditing event, and then update your textbox in that handler.

<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;
}
womp
It always returns an empty string????????
Sauron
Sorry, the code example was kind of generic. You'll need to change the cell that it's referencing to be your actual cell... looks like it's in your first column, not the 6th, and you'll need to get the value of the label control. I'll update the code.
womp
I have a doubt. Since my grid contain only one item can I put it in DataKeyNames, Is there any performance issue?
Sauron
A: 

I got the solution from river-blue's answer.

Add DataKeyNames="Item" to Gridview and inside the edit button click handler

     protected void OnClickEdit( object sender, EventArgs e )
    {
        GridViewRow row = ( (ImageButton) sender ).Parent.Parent as GridViewRow;
        txtName.Text =  grdTrackedItems.DataKeys[row.RowIndex].Value.ToString();
    }
Sauron