繁体   English   中英

Asp.net在PostBack上动态地重新创建列表框

[英]Asp.net Dynamically recreating ListBoxes on PostBack

我想基于主类别和子类别创建动态添加的ListBoxes ,其中它们可以是无限数量的子类别。 我整个星期都在努力,无法解决这个问题。

我希望它像Ebay类别选择。 页面加载后,将出现一个包含所有主要类别的列表框。 当用户在列表框中选择一个项目时,应动态添加另一个列表框,以便每个列表框都具有选定的项目。 因此,在选择第一个列表框中的第一项之后,应该有两个列表框,第二个列表框显示属于所选一个的所有子类别。 请看截图。

Ebay类别选择示例http://www.aquariumbids.com/Images/ebayCat.JPG

每次请求的代码 -我认为它接近工作。

    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>

如果要在没有回发的情况下执行此操作,那么以下文章可能会有所帮助:使用Microsoft ASP.NET AJAX框架创建级联下拉列表的步骤http://support.microsoft.com/kb/976156

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM