ASP.net动态下拉列表,如何使用JQuery和AJAX代替?

时间:2009-10-08 15:29:12

标签: asp.net jquery ajax

我编写了一些使用ASP.Net,C#和AJAX更新面板的代码。 我创建了这个用户控件,其中一个转发器由BusinessTier数据表的内容填充。对于每个业务层,都有相关的业务级别,这些业务级别从BusinessLevel数据表填充下拉列表。 一个业务级别是一组业务层的父级,业务层指示每个业务级别集合在层次结构中的位置。第1层是公司,业务层是公司的名称。第2层是业务,第2层的关联业务层将包含在第1层中分配的父业务层。依此类推。我希望这是有道理的! 这些业务层下拉列表是级联下拉列表。但是,无法使用AJAX级联下拉列表控件,因为在初始表单加载中,将在运行时分配许多这些下拉列表。此外,还会在运行时动态确定下拉数,具体取决于数据库的内容。 因此,对于每个SelectedIndexChanged事件,都必须有一个回发来重新计算下拉列表的新内容。 以下代码有效。这些回发可以包含在更新面板中,并且控件正常,我可以使用它。 但是...... 必须有一个更好的方法使用AJAX和JQuery,而不是首先没有所有这些回发?如果是这样的话? 我想了解更多关于AJAX和JQuery的知识,所以如果你有一个解决方案,我会很高兴看到它。 代码如下;

<%@ Control Language="C#" AutoEventWireup="true" 
     CodeBehind="BusinessLevelSelection.ascx.cs" 
                Inherits="TestLinq.BusinessLevelSelection" %>
<div id="mainleft">
  <fieldset style="width:200px;padding:3px">
      <legend>Business Level</legend>  
      <asp:UpdatePanel runat="server" ID="updBusinessLevels">
      <ContentTemplate>
      <asp:Repeater runat="server" ID="rpDropDowns" 
        OnItemDataBound="rpDropDowns_OnItemDataBound"
        >
        <ItemTemplate>
            <h2><%#  DataBinder.Eval(Container.DataItem, "NameFull")%></h2>
            <div>
                <asp:DropDownList runat="server" ID="ddlBusinessLevels" 
                 AutoPostBack="true" 
                    OnSelectedIndexChanged="ddlBusinessLevels_SelectedIndexChanged"
                    ToolTip='<%#  DataBinder.Eval(Container.DataItem, "NameFull")%>'
                />
            </div>
        </ItemTemplate>
      </asp:Repeater></ContentTemplate></asp:UpdatePanel>
  </fieldset>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibraryBusinessLevels;

namespace TestLinq
{
    public partial class BusinessLevelSelection : System.Web.UI.UserControl
    {
        public int SelectedBusinessLevelId
        {
            get;
            set;
        }

        public int? ParentBusinessLevelId
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LoadData();
        }

        private void LoadData()
        {
            BOL.GetBusinessLevelTiers();
            LoadDataLatest();
        }

        /// <summary>
        /// LoadDataLatest() - Loads the latest data, either from the initial load, 
        ///                     or from the change in the business level selection event
        /// </summary>
        private void LoadDataLatest()
        {
            BOL.GetBusinessLevels(this.SelectedBusinessLevelId);

            this.ParentBusinessLevelId = null;

            rpDropDowns.DataSource = BOL.blTiers;
            rpDropDowns.DataBind();
        }

        protected void rpDropDowns_OnItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            // Execute the following logic for Items and Alternating Items.
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PopulateDropDownList(e.Item.FindControl("ddlBusinessLevels") as DropDownList, e.Item.ItemIndex);
            }
        }

        private void PopulateDropDownList(DropDownList ddlBusinessLevels, int index)
        {
            if (index <= BOL.blCollection.Count)
            {
                ddlBusinessLevels.DataSource =
                    BOL.GetBusinessLevelsForDropDowns(index, this.ParentBusinessLevelId);
                ddlBusinessLevels.DataValueField = "BusinessLevelId";
                ddlBusinessLevels.DataTextField = "BusinessLevelName";
                ddlBusinessLevels.DataBind();

                if (index < BOL.blCollection.Count)
                {
                    ddlBusinessLevels.SelectedValue = BOL.blCollection[index].BusinessLevelId.ToString();
                    this.ParentBusinessLevelId = BOL.blCollection[index].BusinessLevelId;
                }
            }
            ddlBusinessLevels.Items.Insert(0, new ListItem("Please enter your selection", "0"));
        }

        protected void ddlBusinessLevels_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlBusinessLevels = (DropDownList)sender;
            this.SelectedBusinessLevelId = Convert.ToInt32(ddlBusinessLevels.SelectedValue);
            this.LoadDataLatest();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是如何将jQuery与Cascading DropDownLists一起使用:http://www.mikesdotnetting.com/Article/97/Cascading-DropDownLists-with-jQuery-and-ASP.NET