how to check which row button is clicked inside Nested Datagrid

五迷三道 提交于 2019-12-13 03:03:43

问题


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


回答1:


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
    }

}


来源:https://stackoverflow.com/questions/26018237/how-to-check-which-row-button-is-clicked-inside-nested-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!