2

ListBoxesメイン カテゴリとサブカテゴリに基づいて動的に追加されたサブカテゴリを無限に作成したいと考えています。私は一週間ずっとこれに取り組んできましたが、これを理解できません。

Ebayのカテゴリーセレクションみたいにしたいです。ページが読み込まれると、すべてのメイン カテゴリを含むリスト ボックスが表示されます。ユーザーがリストボックス内のアイテムを選択すると、別のリストボックスが動的に追加され、各リストボックスに選択されたアイテムがまだそこにあるようにする必要があります。したがって、最初のリストボックスで最初の項目を選択すると、2 つのリストボックスが表示され、2 番目のリストボックスには、選択したものに属するすべてのサブカテゴリが表示されます。スクリーンショットをご覧ください。

Ebay カテゴリーの選択例 http://www.aquariumbids.com/Images/ebayCat.JPG

CODE PER REQUEST - ほぼ機能していると思います。

    public partial class WebForm2 : System.Web.UI.Page
{
    private Int32 controlCount = 0;
    Panel _panel;

    private Panel PanelPlaceholder
    {
        get
        {
            if (_panel == null && Master != null)
                _panel = pnlContainer;
            return _panel;
        }
    }

    protected void Page_PreInit(Object sender, EventArgs e)
    {
        this.EnsureChildControls();

        if (IsPostBack)
        {
            // Re-create controls but not from datasource
            // The controlCount value is output in the page as a hidden field during PreRender.
            controlCount = Int32.Parse(Request.Form["controlCount"]); // assigns control count from persistence medium (hidden field)         
            for (Int32 i = 0; i < controlCount; i++)
            {
                CreateDynamicControlGroup(false);
            }
        }
    }
    protected void Page_Load(Object sender, EventArgs e)
    {
        // create from data query.
        // only if not postback
        if (!IsPostBack)
        {
            int cc = controlCount;

            DataTable dt = null;                
            Dictionary<string, string> Params = new Dictionary<string, string>();
            dt = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetMainCategories, Params);

            // create a set of dynamic controls for the Row, incrementing counter and 
            // getting a reference to the new controls via their common parent (Dynamic PlaceHolder)
            CreateDynamicControlGroup(true);

            ListBox lb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];

            // On reload you will see that child ListItems are persisted by the DropDownList
            lb.DataSource = dt; // use the same table
            lb.DataValueField = "ID";
            lb.DataTextField = "Name";
            lb.DataBind();
        }
    }


    protected void Page_PreRender(Object sender, EventArgs e)
    {
        // persist control count
        ClientScript.RegisterHiddenField("controlCount", controlCount.ToString());
    }


    private void ListBox_SelectedIndexChanged(Object sender, EventArgs e)
    {
        ListBox lb = sender as ListBox;


        Dictionary<string, string> Params = new Dictionary<string, string>();
        Params.Add("parentID", lb.SelectedValue);
        DataTable Categories = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetChildCategories, Params);

        if (Categories.Rows.Count > 0)
        {
            // create a set of dynamic controls for the Row, incrementing counter and 
            // getting a reference to the new controls via their common parent (Dynamic PlaceHolder)
            CreateDynamicControlGroup(true);

            ListBox newLb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];
            // On reload you will see that child ListItems are persisted by the DropDownList
            newLb.DataSource = Categories; // use the same table
            newLb.DataValueField = "ID";
            newLb.DataTextField = "Name";
            newLb.DataBind();
        }
    }


    private void CreateDynamicControlGroup(Boolean incrementCounter)
    {
        // Create one logical set of controls do not assign values!
        ListBox lb = new ListBox();
        lb.AutoPostBack = true;
        lb.CssClass = "panel";
        PanelPlaceholder.Controls.Add(lb);

        // wire event delegate
        lb.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);

        if (incrementCounter)
        {
            controlCount += 1;
        }
    }
}  

マークアップ:

 <div class="Column12" id="Form_NewListing">
    <h2 class="h2row">Create Your Listing - Step 1 of 2)</h2>
    <h3 class="h3row">Select a category</h3>
    <div class="panel">
        <asp:Panel ID="pnlContainer" runat="server"></asp:Panel>        

    </div>
</div>
4

1 に答える 1