Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C#
Expertise: Beginner
Apr 25, 2005

Access GridView Container Row Items from the Button Click Event

Assume Each GridViewRow contains Field1, Field2,.... Button:

<asp:GridView ID="GridView1" Runat="server" DataSourceID="sdsDataSource">
   <asp:BoundField  DataField="Field1" HeaderText="Field1"></asp:BoundField>
   <asp:BoundField  DataField="Field2" HeaderText="Field2"></asp:BoundField>
   <asp:Button ID="Button1" Runat=Server OnClick="Button1_Click" />
To access the other items in the same row from inside the button click event, you need to find the corresponding GridViewRow in which the button is placed.

GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;

   void Button1_Click(object o, EventArgs e)
   Button Button1 = (Button)o;
   GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
   string strField1 = grdRow.Cells[0].Text;
   string strField2 = grdRow.Cells[0].Text;
Note: Applicable only for ASP.NET 2.0.

Jaya Nehru Kumar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.