开发者

Nested GridView - How to trigger the Child GridView Button Click event

开发者 https://www.devze.com 2023-04-11 21:01 出处:网络
I need to show a Master/Child data in a page and I have used multiple GridViews to achieve the same. So, I have created two GridViews (Parent & Child) and now I want to fire the Button click event

I need to show a Master/Child data in a page and I have used multiple GridViews to achieve the same. So, I have created two GridViews (Parent & Child) and now I want to fire the Button click event (i.e. btnLock) from the child gridview control and do some DB operations. So, I dont know how to achieve this.

开发者_如何学运维

Please help.

<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:GridView Width="100%" AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False"
DataSourceID="sqlDsCustomers" runat="server" ShowHeader="False" OnRowCreated="gvCustomers_RowCreated">
<Columns>
<asp:TemplateField>
<ItemTemplate>

    <div class="group" id='<%#String.Format("customer{0}",Container.DataItemIndex) %>' onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>,<%#String.Format("\"#order{0}\"",Container.DataItemIndex) %>)'>
        <asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png"
            Style="margin-right: 5px;" runat="server" /><span class="header">
                <%#Eval("CustomerID")%>
                :
                <%#Eval("CompanyName")%>
                (<%#Eval("TotalOrders")%>
                Orders) </span>
    </div>
    <asp:SqlDataSource ID="sqlDsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
        SelectCommand="SELECT [OrderID], [OrderDate], [RequiredDate], [Freight], [ShippedDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
        <SelectParameters>
            <asp:Parameter Name="CustomerID" Type="String" DefaultValue="" />
        </SelectParameters>
    </asp:SqlDataSource>
    <div id='<%#String.Format("order{0}",Container.DataItemIndex) %>' class="order">
        <asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
            runat="server" ShowHeader="true" EnableViewState="false">
            <RowStyle CssClass="row" />
            <AlternatingRowStyle CssClass="altrow" />
            <Columns>
                <asp:TemplateField ItemStyle-CssClass="rownum">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
                <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
                    ItemStyle-Width="100px" />
                <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                    ItemStyle-Width="110px" />
                <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
                    ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
                    ItemStyle-Width="100px" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnLock" Text="Lock" CommandName="Lock" CommandArgument=<%# Eval("OrderID") %> runat="server" />
                        </ItemTemplate>    
                    </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

Thanks


Use the RowCommand:

 <asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
            runat="server" ShowHeader="true" EnableViewState="false"
            onrowcommand="gvOrders_RowCommand"
   >
   ........
 </asp:GridView >


 protected void gvOrders_RowCommand(Object sender, GridViewCommandEventArgs e)
 {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Lock")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
      //dowork

    }
 }    
0

精彩评论

暂无评论...
验证码 换一张
取 消