0

I've tried all the suggestions I've found here and elsewhere. I've set EnableViewState to true and false for the page. I've set EnableViewState to true and false for the grids. I've tried registering the events for the child grid in the parent grid RowCreated. I am truly at a loss. How do I make this work? Neither the parent or child event is firing.

<%@ Control Language="C#" EnableViewState="False" AutoEventWireup="False" CodeBehind="View.ascx.cs" Inherits="IMS.Modules.CreateMerchandise.View" %>


<asp:GridView ID="gvVariants"
              runat="server"
              AutoGenerateColumns="False"
              CssClass="Grid"
              DataKeyNames="ID"
              OnRowDataBound="gvVariants_OnRowDataBound"
              ShowFooter="True"
              EnableViewState="False"
              OnRowCreated="gvVariants_OnRowCreated"
              OnRowCommand="gvVariants_OnRowCommand">
    <Columns>
        <asp:TemplateField InsertVisible="False">
            <ItemStyle HorizontalAlign="Center" Width="30"/>
            <ItemTemplate>
                <img alt="" style="cursor: pointer; height: 20px; width: 20px;" src="../images/ManageMerchandise/plus.png"/>
                <asp:Panel ID="pnlValues" runat="server" Style="display: none" EnableViewState="False">
                    <asp:GridView
                        ID="gvValues"
                        runat="server"
                        AutoGenerateColumns="False"
                        CssClass="ChildGrid"
                        DataKeyNames="ID"
                        ShowFooter="True"
                        EnableViewState="False"
                        OnRowCommand="gvValues_OnRowCommand">
                        <Columns>
                            <asp:TemplateField ItemStyle-Width="15px">
                                <FooterTemplate>
                                    <asp:Button runat="server" text="Add" ID="btnAddValue" CommandName="AddValue" CommandArgument='<%# Bind("ID") %>'/>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField
                                HeaderText="Value"
                                InsertVisible="True"
                                ItemStyle-Width="150px">
                                <ItemTemplate>
                                    <asp:Label ID="lblValue" runat="server"
                                               Text='<%# Bind("VariantValue") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="NewValue" runat="server" Width="150"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Use">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbUseVariantValue" runat="server"
                                                  Enabled="true"/>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:CheckBox ID="cbUseNewVariantValue" runat="server"/>
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button runat="server" text="Add" ID="btnAddVariant" CommandName="addVariant" CommandArgument='<%# Bind("ID") %>'/>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Variant" InsertVisible="True">
            <ItemTemplate>
                <asp:Label ID="lblVarietal" runat="server"
                           Text='<%# Bind("Name") %>'>
                </asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" Width="240"/>
            <FooterTemplate>
                <asp:TextBox ID="NewVariant" runat="server"></asp:TextBox>
            </FooterTemplate>
            <FooterStyle Wrap="False"/>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Use">
            <ItemTemplate>
                <asp:CheckBox ID="cbUseVariant" runat="server"
                              Enabled="true"/>
            </ItemTemplate>
            <FooterTemplate>
                <asp:CheckBox ID="cbUseNewVariant" runat="server"/>
            </FooterTemplate>
            <ItemStyle HorizontalAlign="Center"/>
            <FooterStyle HorizontalAlign="Center"/>
        </asp:TemplateField>
    </Columns>
</asp:GridView>OnLoad:

OnLoad:

protected override void OnLoad(EventArgs e)
{
        try
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                gvVariants.DataSource = GetVariantTable();
                gvVariants.DataBind();
            }
        }
        catch (Exception exc) //Module failed to load
        {
            DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
        }

    }

RowDataBound in codebehind:

    protected void gvVariants_OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {         
            String variantId= gvVariants.DataKeys[e.Row.RowIndex].Value.ToString();

            GridView gvValues = e.Row.FindControl("gvValues") as GridView;
            using (var ipv = new IMM_ProductVariantValuesController())
            {
                ipv.Gets(variantId,"IMM_ProductVariants_ID",pWhere: $"and (portal_id=-1 or portal_id={PortalId})");
                ipv.LoadDatatable();
                gvValues.DataSource = ipv.Datatable;
                gvValues.DataBind();
            }
        }      
    }

    protected void gvVariants_OnRowCommand(Object sender, GridViewCommandEventArgs e)
    {
        Response.Write("got here");
        Response.End();
    }
Chris
  • 650
  • 7
  • 20

2 Answers2

0

There are several issues that cause problems.

The Add button has a onClick event, but you also use onRowCommand in each grid. When the Add button is clicked three events are fired (button click, parent grid rowcommand, nested grid rowcommand).

It looks like you are using a global variable VariantID. This works for the CommandArgument, but should you use it in the HTML of a page, it would still only contain the ID-value of the last added row. So it works but it is not recommended. Better use something like this CommandArgument='<%# Bind("itemID") %>'

You use the VariantID in the CommandArgument attribute wrong in the Add button. you use CommandArgument="<%=VariantID%>" so the command argument is the string <%=VariantID%>. You use it like this CommandArgument='<%# VariantID %>'

You are binding the grid outside the try-catch block on OnLoad(EventArgs e). When there is an error it will crash the complete DotNetNuke page instead of firing the ModuleLoadException. Although this could be for testing purposes.

Fix these issues first and see if it helps. It did on my development server.

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Same thing. Some more info here I should have added before. When I click the button, some event fires. OnLoad is executed, skipping (!PostBack). The page is reloaded but the grid is not visible because it seems to be losing its binding someplace. – Chris Aug 27 '16 at 15:00
  • I've edited the question including the changes you've suggested. – Chris Aug 27 '16 at 15:01
0

And this now works:

<%@ Control Language="C#" EnableViewState="False" ViewStateMode="Disabled" AutoEventWireup="False" CodeBehind="View.ascx.cs" Inherits="IMS.Modules.CreateMerchandise.View" %>

<style type="text/css">
    body {
        font-family: Arial;
        font-size: 10pt;
    }

    .Grid td {
        background-color: #A1DCF2;
        color: black;
        font-size: 10pt;
        line-height: 200%
    }

    .Grid th {
        background-color: #3AC0F2;
        color: White;
        font-size: 10pt;
        line-height: 200%
    }

    .ChildGrid td {
        background-color: #eee !important;
        color: black;
        font-size: 10pt;
        line-height: 200%
    }

    .ChildGrid th {
        background-color: #6C6C6C !important;
        color: White;
        font-size: 10pt;
        line-height: 200%
    }
</style>


<script type="text/javascript">

    function SetID(id) {
        document.getElementById("hfVariantID").value = id;
    }

    $("[src*=plus]")
        .live("click",
            function() {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
                $(this).attr("src", "../images/ManageMerchandise/minus.png");
            });
    $("[src*=minus]")
        .live("click",
            function() {
                $(this).attr("src", "../images/ManageMerchandise/plus.png");
                $(this).closest("tr").next().remove();
            });
</script>
<asp:UpdatePanel runat="server" ID="upVariants" UpdateMode="Conditional" ViewStateMode="Disabled" EnableViewState="False">
<ContentTemplate>
<asp:GridView ID="gvVariants" runat="server" AutoGenerateColumns="False" CssClass="Grid" DataKeyNames="ID" OnRowDataBound="gvVariants_OnRowDataBound" ShowFooter="True" EnableViewState="False" OnRowCommand="gvVariants_OnRowCommand">
    <Columns>
        <asp:TemplateField InsertVisible="False">
            <ItemStyle HorizontalAlign="Center" Width="30"/>
            <ItemTemplate>
                <img alt="" style="cursor: pointer; height: 20px; width: 20px;" src="../images/ManageMerchandise/plus.png"/>
                <asp:Panel ID="pnlValues"  runat="server" Style="display: none" EnableViewState="False">
                    <asp:GridView ID="gvValues" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvValues_OnRowDataBound" CssClass="ChildGrid" DataKeyNames="ID" ShowFooter="True" EnableViewState="False">
                        <Columns>
                            <asp:BoundField DataField="ID" Visible="False"/>
                            <asp:TemplateField ItemStyle-Width="15px">
                                <FooterTemplate>
                                    <asp:Button runat="server" text="Add" ID="btnAddValue" OnClick="btnAddValue_Click" CommandName="AddValue" CommandArgument='<%# Bind("ID") %>'/>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField
                                HeaderText="Value"
                                InsertVisible="True"
                                ItemStyle-Width="150px">
                                <ItemTemplate>
                                    <asp:Label ID="lblValue" runat="server"
                                               Text='<%# Bind("VariantValue") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="NewValue" runat="server" Width="150"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Use">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbUseVariantValue" runat="server"
                                                  Enabled="true"/>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:CheckBox ID="cbUseNewVariantValue" runat="server"/>
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button runat="server"  text="Add"  ID="btnAddVariant" CommandName="addVariant" OnClick="btnAddVariant_Click" CommandArgument='<%# Bind("ID") %>'/>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Variant">
            <ItemTemplate>
                <asp:Label ID="lblVarietal" runat="server"
                           Text='<%# Bind("Name") %>'>
                </asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" Width="240"/>
            <FooterTemplate>
                <asp:TextBox ID="NewVariant" runat="server"></asp:TextBox>
            </FooterTemplate>
            <FooterStyle Wrap="False"/>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Use">
            <ItemTemplate>
                <asp:CheckBox ID="cbUseVariant" runat="server"
                              Enabled="true"/>
            </ItemTemplate>
            <FooterTemplate>
                <asp:CheckBox ID="cbUseNewVariant" runat="server"/>
            </FooterTemplate>
            <ItemStyle HorizontalAlign="Center"/>
            <FooterStyle HorizontalAlign="Center"/>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField runat="server" ID="hfVariantID" ClientIDMode="Static"/>

CodeBehind:

/*
' Copyright (c) 2016  Indy Music Systems
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using IMS.Model;

namespace IMS.Modules.CreateMerchandise
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    ///     The View class displays the content
    ///     Typically your view control would be used to display content or functionality in your module.
    ///     View may be the only control you have in your project depending on the complexity of your module
    ///     Because the control inherits from CreateMerchandiseModuleBase you have access to any custom properties
    ///     defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public  partial class View : CreateMerchandiseModuleBase, IActionable
    {
        public static readonly String StrPortalId =
            PortalController.Instance.GetCurrentPortalSettings().PortalId.ToString();
        public String curVariantId="";
        public GridView gvStore;
        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                {
                    {
                        GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                        EditUrl(), false, SecurityAccessLevel.Edit, true, false
                    }
                };
                return actions;
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                gvVariants.DataSource = GetVariantTable(StrPortalId);
                gvVariants.DataBind();
                //Session["gvStore"] = gvVariants;
                Session["gvStore"] = gvVariants.DataSource;
            }
            else
            {
                //gvVariants = (GridView)Session["gvStore"];
                //gvVariants.DataSource = (DataTable)((GridView)Session["gvStore"]).DataSource;
                gvVariants.DataSource = (DataTable)Session["gvStore"];
                gvVariants.DataBind();
            }
        }


        protected void gvVariants_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {         
                //curVariantId= gvVariants.DataKeys[e.Row.RowIndex].Value.ToString();
                curVariantId = ((DataRowView) e.Row.DataItem)[0].ToString();
                GridView gvValues = e.Row.FindControl("gvValues") as GridView;
                using (var ipv = new IMM_ProductVariantValuesController())
                {
                    ipv.Gets(curVariantId,"IMM_ProductVariants_ID",pWhere: $"and (portal_id=-1 or portal_id={PortalId})");
                    if (!ipv.Recordset.Any())
                    {
                        ipv.Recordset.Add(new IMM_ProductVariantValues()
                        {
                            ID=-1
                        });
                    }
                    ipv.LoadDatatable();
                    gvValues.DataSource = ipv.Datatable;
                    gvValues.DataBind();
                }
            }      
        }

        protected void gvValues_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)

            {
                if (((DataRowView)e.Row.DataItem)[0].ToString() == "-1")
                {
                    e.Row.Visible = false;
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Button b = e.Row.FindControl("btnAddValue") as Button;
                b.OnClientClick = "SetID('" +curVariantId+"')";
            }
        }

        protected void btnAddValue_Click(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);

            string myID = hfVariantID.Value;
            TextBox txtNewValue = (TextBox)row.FindControl("NewValue");
            using (var ipvv = new IMM_ProductVariantValuesController())
            {
                ipvv.Recordset.Add(new IMM_ProductVariantValues()
                {
                    IMM_ProductVariants_ID = Convert.ToInt32(myID),
                    Portal_ID = Convert.ToInt32(StrPortalId),
                    VariantValue = txtNewValue.Text.Trim().TrimStart(',') //remove leading PostBack comma
            });
                ipvv.Inserts();
            }
        }

        protected void btnAddVariant_Click(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
            TextBox txtNewValue = (TextBox)row.FindControl("NewVariant");
            using (var ipv = new IMM_ProductVariantsController())
            {
                ipv.Recordset.Add(new IMM_ProductVariants()
                {
                    Portal_ID = Convert.ToInt32(StrPortalId),
                    Name = txtNewValue.Text.Trim().TrimStart(',') //remove leading PostBack comma
                });
                ipv.Inserts();
            }
        }

        protected void gvVariants_OnRowCommand(Object sender, GridViewCommandEventArgs e)
        {
            gvVariants.DataSource = GetVariantTable(StrPortalId);
            gvVariants.DataBind();
            Session["gvStore"] = gvVariants.DataSource;
        }

    }
}
Chris
  • 650
  • 7
  • 20