-1

我在使用另一个 gridview 中的 gridview 行命令时遇到问题

问题是,当我将事件添加到另一个gridview 的项目模板中的第二个gridview 时,该事件不会运行,我怎样才能使它工作?

我的代码是:

<asp:ToolkitScriptManager runat="server" ID="MainScriptManager" />       
<asp:SqlDataSource ID="sqlDsSubCategories" runat="server"
    ConnectionString="<%$ ConnectionStrings:Scan_SystemConnectionString %>"
    SelectCommand="SP_Categoria" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter DefaultValue="01" Name="div" SessionField="DivNom" 
            Type="String" />
        <asp:SessionParameter DefaultValue="04" Name="mes" SessionField="mes" 
            Type="String" />
        <asp:SessionParameter DefaultValue="2013" Name="year" SessionField="year" 
            Type="String" />
            <asp:SessionParameter DefaultValue="042013" Name="fecha" SessionField="fecha" 
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:UpdatePanel ID="pnlUpdate" runat="server">
    <ContentTemplate>
        <asp:GridView Width="100%" ID="gvSubCategories"
                    AutoGenerateColumns="False"
                    GridLines="None"                   
                    CssClass="mGrid"
                    DataSourceID="sqlDsSubCategories" runat="server"
                    ShowHeader="False"
                    OnRowCreated="gvSubCategories_RowCreated"
                    DataKeyNames="CG_CATEGORIA">                                               
            <Columns>
                <asp:TemplateField>
                    <ItemStyle Width="200px" />
                    <ItemTemplate>                                                       
                        <asp:Panel ID="pnlSubCategories" runat="server">
                            <asp:Image ID="imgCollapsible"
                                Style="margin-right: 5px;" runat="server" />
                             <span style="font-weight:bold">                                           
                                    SubCategory: <%# Eval("CG_CATEGORIA")%> | Total: $<%# Eval("dist_amt")%></span></asp:Panel>
                      <asp:SqlDataSource ID="sqlDsProducts" runat="server"
                         ConnectionString="<%$ ConnectionStrings:Scan_SystemConnectionString %>"
                  SelectCommand="SP_VoucherPorDiv" SelectCommandType="StoredProcedure">
    <SelectParameters>
       <asp:SessionParameter DefaultValue="01" Name="div" SessionField="DivNom" 
            Type="String" />
        <asp:SessionParameter DefaultValue="04" Name="mes" SessionField="mes" 
            Type="String" />
        <asp:SessionParameter DefaultValue="2013" Name="year" SessionField="year" 
            Type="String" />
            <asp:SessionParameter DefaultValue="042013" Name="fecha" SessionField="fecha" 
            Type="String" />
        <asp:Parameter DefaultValue="" Name="cat" Type="String" />
    </SelectParameters>
                        </asp:SqlDataSource>
                        <asp:Panel ID="pnlProducts" runat="server"
                                   Width="75%"
                            Style="margin-left:20px;margin-right:20px;
                                   height:0px;overflow: hidden;">                                   
                            <asp:GridView Width="90%" AutoGenerateColumns="False"
                                CssClass="mGrid" ID="gvProducts" HorizontalAlign="Center" PagerStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center"
                                DataSourceID="sqlDsProducts"
                                runat="server" EnableViewState="False" 
                                GridLines="None"                                      
                                AlternatingRowStyle-CssClass="alt">                                       
                               <Columns>                                  
                                    <asp:BoundField HeaderText="Voucher"
                                              DataField="vchr_or_chk_no"/>
                                    <asp:BoundField
                                        HeaderText="Reference"
                                        DataField="reference"/>
                                        <asp:BoundField
                                        HeaderText="Quantity"
                                        DataField="dist_amt"/>
                                        <asp:TemplateField HeaderText="Consult">
                        <ItemTemplate>
                            <asp:LinkButton ID="verpdf" runat="server" CommandName="Select">View</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

                                </Columns>
                            </asp:GridView>
                        </asp:Panel>     
                        <asp:CollapsiblePanelExtender
                           ID="ctlCollapsiblePanel"
                            runat="Server"
                            TargetControlID="pnlProducts"
                            CollapsedSize="0" Collapsed="True"
                            ExpandControlID="pnlSubCategories"
                            CollapseControlID="pnlSubCategories"
                            AutoCollapse="False" AutoExpand="False"
                            ScrollContents="false"
                            ImageControlID="imgCollapsible"
                            ExpandedImage="~/collapse.png"
                            CollapsedImage="~/expand.png"
                            ExpandDirection="Vertical" />                                                
                    </ItemTemplate>
                </asp:TemplateField>   
            </Columns>
        </asp:GridView>                                           
    </ContentTemplate>
</asp:UpdatePanel>

Protected Sub gvSubCategories_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSubCategories.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ctrl As SqlDataSource = TryCast(e.Row.FindControl("sqlDsProducts"), SqlDataSource)
            If ctrl IsNot Nothing AndAlso e.Row.DataItem IsNot Nothing Then
                ctrl.SelectParameters("cat").DefaultValue = gvSubCategories.DataKeys(e.Row.RowIndex).Value.ToString()
            End If
        End If

    End Sub
4

1 回答 1

0

您必须连接子 GridView 的 RowCommand;您可以在父 GridView 的 RowCreated 事件中编写此代码,因为此事件在 Page_Load 之后和处理回发事件之前触发。

例如在 C# 中:

protected void parentGrid_RowCreated(Object sender, GridViewRowEventArgs e)

    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            childGrid = e.Row.FindControl("childGrid") as GridView;
            childGrid.RowCommand += new GridViewCommandEventHandler(childGrid_RowCommand);
        }
    }
于 2013-05-14T23:25:23.183 回答