3

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

rick schott
  • 21,012
  • 5
  • 52
  • 81
user972255
  • 1,828
  • 10
  • 32
  • 52

1 Answers1

0

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

    }
 }    
rick schott
  • 21,012
  • 5
  • 52
  • 81
  • I tried your suggestion but the code is not getting fired. I also debugged but the code is not getting reached. – user972255 Oct 07 '11 at 01:12
  • Actually I have removed the EnableViewState="false" and it works properly. But is there any way without using viewstate we can fire the events? Because my data is huge and when I carry all these things my page will become very slow. – user972255 Oct 07 '11 at 01:15
  • I don't have a refernce for you but not all controls can fire events without ViewState. You can leave your ViewState on the server. http://blogs.msdn.com/b/alikl/archive/2008/01/08/how-to-keep-asp-net-viewstate-on-the-server-revised.aspx – rick schott Oct 07 '11 at 01:25