简体   繁体   中英

how to check which row button is clicked inside Nested Datagrid

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

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM