2007年3月26日星期一

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"> 
<Columns>
<asp:BoundField DataField="Field1" HeaderText="Field1">
</asp:BoundField>
<asp:BoundField DataField="Field2" HeaderText="Field2">
</asp:BoundField> . . .
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" Runat=Server OnClick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
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.
 
void Button1_Click(object o, EventArgs e)   
{ 
GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
Button Button1 = (Button)o;
GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
string strField1 = grdRow.Cells[0].Text; string strField2 = grdRow.Cells[0].Text; ... }

没有评论: