65.9K
CodeProject 正在变化。 阅读更多。
Home

运行时依赖的 ListBox

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (3投票s)

2007 年 5 月 29 日

1分钟阅读

viewsIcon

40465

downloadIcon

791

本文介绍了如何在运行时使用 Ajax(MagicAjax)添加依赖 ListBox。

Screenshot - RuntimeDependentListBox.gif

引言

有时,我需要一个依赖于另一个ListBox的ListBox。而且不仅仅是每个表单一个,而是每个表单需要的数量。本文解释了如何实现这一点。

使用代码

这段代码使用 MagicAjax 来处理数据,而无需发布整个页面。AddDependentListBox() 函数在运行时创建一个 MagicAjax AjaxPanel。DropDownList 将其添加到 AjaxPanel,然后将 AjaxPanel 添加到页面。在此示例中,使用了占位符,但表或任何其他控件也足够了。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MagicAjax;

namespace RuntimeDependentListBox
{
    public class DependentListBox : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.PlaceHolder ph;

        private void Page_Load(object sender, System.EventArgs e)
        {
            //Put user code to initialize the page here

            //AddDependentListBox(Unique Key to find 

            //each dependent list box you want to add)

            //you can add as much as you want as long as 

            //you don't repeat the Key)


            AddDependentListBox("Example_Key");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //

            //CODEGEN: This call is required by the 

            //ASP.NET Web Form Designer.

            //

            InitializeComponent();
            base.OnInit(e);
        }

        /// 

        <summary />/// Required method for Designer support - do not modify</summary />

        <summary />/// the contents of this method with the code editor.</summary />

        <summary />/// </summary />

        <summary /></summary />private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

注意:创建第一个 DropDownList 时,也会创建一个 SelectedIndexChange 的事件处理程序。这提供了根据第一个 DropDownList 中选择的值对第二个 DropDownList 进行数据绑定的功能。由于在 AddDependentListBox() 函数中提供了 UniqueKey,这让事件处理程序可以通过 ID 知道要填充哪个 DropDownList。反过来,这让用户可以添加任意数量的运行时依赖的ListBox。

        private void AddDependentListBox(string pstrKey)
        {
            //creates a MagicAjax AjaxPanel in Runtime that is unique for 

            //each DropDownList

            MagicAjax.UI.Controls.AjaxPanel ap = 
                new MagicAjax.UI.Controls.AjaxPanel();

            //This lines creates the First DropDownlist in Runtime

            DropDownList cboFirst = new DropDownList();
            cboFirst.ID = "1_" + pstrKey;
            cboFirst.AutoPostBack = true;

            //this add the EventHandler Function to the 

            //First Dependent List Box

            cboFirst.SelectedIndexChanged += new EventHandler(
                cboFirst_SelectedIndexChanged);


            //Creation of teh second DropDownlist in RunTime

            DropDownList cboSecond = new DropDownList();
            cboSecond.ID = "2_" + pstrKey;
            cboSecond.Enabled = false;

            //Both DropDownLists are added to the 

            //MagicAjax AjaxPanel that was created

            ap.Controls.Add(cboFirst);
            ap.Controls.Add(cboSecond);

            //This line add the Created DropDownList to the PlaceHolder

            ph.Controls.Add(ap);

            //This Line Populates First DropDownlist

            PopulateFirst(pstrKey);
        }


        private void PopulateFirst(string pstrKey)
        {
            //After adding to the PlaceHolder, you can find the 

            //created DropDownList in the Page with FindControl

            //and populate it as you want

            DropDownList cboFirst = (DropDownList)Page.FindControl(
                "1_" + pstrKey);
            cboFirst.Items.Clear();
            cboFirst.Items.Add(new ListItem("Select",""));
            cboFirst.Items.Add(new ListItem("First Value","1"));
            cboFirst.Items.Add(new ListItem("Second Value","2"));
        }

        private void cboFirst_SelectedIndexChanged(object sender, 
            System.EventArgs e)
        {
            //if the index changes run this

            //the DropDownList changed

            DropDownList cboFirst = (DropDownList)sender;

            //Find the unique Key spliting the ID of the DropDownList

            string strKey = cboFirst.ID.Split('_')[1].ToString();

            //the second dropdownlist

            DropDownList cboSecond = (DropDownList)Page.FindControl(
                "2_" + strKey);

            //Poluates the Second DropDowList using the 

            //value selected from the First DropDownList

            if(cboFirst.SelectedIndex>0)
            {
                PopulateSecond(strKey, cboFirst.SelectedValue.ToString());
                cboSecond.Enabled = true;
            }
            else
            {
                cboSecond.Items.Clear();
                cboSecond.Enabled = false;
            }
        }


        private void PopulateSecond(string pstrKey, string pstrcboFirstValue)
        {
            //Populates the Second DropDownList accornding to the 

            //value selected from the first.

            DropDownList cboSecond = (DropDownList)Page.FindControl(
                "2_" + pstrKey);
            cboSecond.Items.Clear();
            if(pstrcboFirstValue=="1")
            {
                cboSecond.Items.Add(new ListItem(
                    "First Value Selected 1","1"));
                cboSecond.Items.Add(new ListItem(
                    "First Value Selected 2","2"));
                cboSecond.Items.Add(new ListItem(
                    "First Value Selected 3","3"));
            }
            else if(pstrcboFirstValue=="2")
            {
                cboSecond.Items.Add(new ListItem(
                    "Second Value Selected 1","1"));
                cboSecond.Items.Add(new ListItem(
                    "Second Value Selected 2","2"));
            }
        }
    }
}

在运行时为每组依赖的ListBox创建 MagicAjax AjaxPanel,这使得 Ajax 能够仅加载位于其 AjaxPanel 内的ListBox,而不会干扰其他ListBox。

结论

好了,实际上就是这样。虽然代码很简单,但许多开发人员对使用动态创建的控件感到奇怪。但是,本文使用简单的代码来改进使用 C# 和 Ajax 的依赖ListBox。

历史

  • v1.0 2007/05/22
© . All rights reserved.