Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am confused with this problem.

I have put a button in side the template field of a gridview and want to return the data from that specific GridView Row when that corresponding button is clicked.
C#
<asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="Button2" CssClass ="btnSkin" runat="server" Text="Answer" 
                            Width="117px" onclick="Button2_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
<


In the button click event fireup, I want to read that data by creating a GridViewRow Element.
C#
protected void Button2_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            Label8.Text = gvr.Cells[1].Text;
            Label10.Text = gvr.Cells[2].Text;
            Label12.Text = gvr.Cells[3].Text;
}


Now the problem is, the GridViewRow Cells are returning empty strings.

What should I do?????
Posted
Updated 15-Sep-13 20:28pm
v2

Try this
C#
protected void Button2_Click(object sender, EventArgs e)
{
 GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
 txt_bx.Text=((TextBox)GridView1.Rows[gvRow.RowIndex].FindControl("id of textbox in gridview")).Text;           
}

Regards..:)
 
Share this answer
 
v3
Comments
Chandan_Roy 16-Sep-13 2:54am    
Thanks Sir. Finally you provided the solution that I was looking for the last 4 months.
Thanks7872 16-Sep-13 2:54am    
Glad to help you. Enjoy..!
Hi,
Check this link ,Hope this will solve your question.

C#
protected void Button2_Click(object sender, EventArgs e)
        {

GridViewRow gvr= (GridViewRow)((Button)sender).NamingContainer;
    Label8.Text = gvr.Cells[1].Text;
            Label10.Text = gvr.Cells[2].Text;
            Label12.Text = gvr.Cells[3].Text;
}


http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html[^]
 
Share this answer
 
Hi,

When ever you want to fire any control event inside gridview, then instead that control's event use gridview's OnRowCommand event.

In your case do following.
ASP.NET
<!--Aspx page-->
<asp:gridview id="YourGridID" runat="server" onrowcommand="YourGridID_RowCommand" ..="" xmlns:asp="#unknown">
      <asp:templatefield>
            <itemtemplate>
                  <asp:button id="Button2" cssclass="btnSkin" runat="server" text="Answer">
                            Width="117px" CommandName="GetRow" />
            </asp:button></itemtemplate>
      </asp:templatefield>
</asp:gridview>

C#
//Code behind
protected void YourGridID_RowCommand(object sender, GridViewCommandEventArgs e)
{
      if (e.CommandName == "GetRow")
      {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            //Here i am assuming you are having label inside your gridview
            Label lbl= (Label)row.FindControl("YourLabelID");
            Label lbl1= (Label)row.FindControl("YourLabelID");
            Label lbl2= (Label)row.FindControl("YourLabelID");

            Label8.Text = lbl.Text;
            Label10.Text = lbl1.Text;
            Label12.Text = lbl2.Text;
      }
}


Hope it helps.
 
Share this answer
 
Comments
Chandan_Roy 16-Sep-13 4:51am    
Thanks for the information. I thought, commandname can only be used with buttonfield, not with template field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900