Reputation: 299
I was Developing an Web application which contains a Data Grid nested with another Data Grid.Where as the Child Data Grid Contains a Button At the End of Each Parent Grid as Shown below:-
What i want is I need to know on Button Click Which Button is clicked.
Here is my aspx code:
<asp:DataGrid ID="dgparent" runat="server" BorderWidth="1px" BorderColor="#FE9B00">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataGrid ID="dgchild" runat="server" >
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="mFCF_NUPKId" Visible="False"></asp:BoundColumn>
<asp:BoundColumn DataField="CostSheetNo" HeaderText="CostSheetNo" SortExpression="CostSheetNo">
</Columns>
</asp:DataGrid>
<table>
<tr>
<td>
<asp:Label ID="LblTotalCoLoaderFrom1" runat="server" Text="Total Cost : "></asp:Label>
<asp:TextBox ID="TxtTotalCoLoaderFrom1" runat="server" Enabled="false"></asp:TextBox>
</td>
<td>
<asp:Label ID="LblTotalYeild" runat="server" Text="Total Yeild : "></asp:Label>
<asp:TextBox ID="TxtTotalYeild" runat="server" Enabled="false"></asp:TextBox>
</td>
<td>
<asp:Button ID="BTNBookingReq" runat="server" class="formbutton" OnClick="BTNBookingReq_Click" Text="Send Booking Request"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
and this is my C#:
protected void BTNBookingReq_Click(Object sender, EventArgs e)
{
Button btnSender = (Button)sender;
//if(btnSender == 1strow)
//Need to get the Parent Column Text
else if(btnSender == 2ndrow)
//Need to get the Parent Column Text
.........
}
can any one please help me to solve this issue. Thanks in Advance
Upvotes: 0
Views: 767
Reputation: 3681
You can get the parent grid item using NamingContainer of the button, which will give the datagrid item. From there you can find each textbox using the FindControl method as below
protected void BTNBookingReq_Click(Object sender, EventArgs e)
{
Button btnSender = (Button)sender;
DataGridItem item = btnSender.NamingContainer as DataGridItem;
if (item != null)
{
TextBox TxtTotalCoLoaderFrom1 = item.FindControl("TxtTotalCoLoaderFrom1") as TextBox;
//Do your operation here
}
}
Upvotes: 1