天天看點

AjaxPro無重新整理選擇清單框/下拉框[友善|穩定]

伺服器端代碼:

/// <summary>

    /// Ajax伺服器端響應方法

    /// </summary>

    /// <param name="param"></param>

    /// <returns></returns>

    [AjaxPro.AjaxMethod]

    public List<List<string>> GetData(string param)

    {

        //獲得資料

        Hashtable ht = DBHelper.GetDDLHashData(param);

        List<List<string>> list = new List<List<string>>();

        List<string> ilist;

        //疊代拷貝資料

        foreach (DictionaryEntry item in ht)

        {

            ilist = new List<string>();

            ilist.Add(item.Key.ToString());

            ilist.Add(item.Value.ToString());

            list.Add(ilist);

        }

        return list;

    }

    代碼基本上不難,用泛型來包裝一個List傳回給用戶端。再看用戶端代碼:

//添加Dropdownlist内容

function AddItemsTosDropdown(array,obj)

{

    try{

        var GetObj=document.getElementById(obj);

        GetObj.length=0;

        GetObj.options.add(new Option("請選擇",""));

        for(var i=0;i<array.length;i++){

            GetObj.options.add(new Option(array[i][1],array[i][0]));

    catch(e){

        alert(e.message);

}

    調用js方法代碼:

function selectChangeData(selectedValue)

{    

    try

        //獲得資料并添加到清單框

        AddItemsTosDropdown(ItemSeach.GetData(String(selectedValue)).value,"<%=ddlControl.ClientID%>");

    }catch(e){

OK!沒有一點問題,複制代碼隻需要改下資料源擷取就可以用了,比較通用,但是别忘了AjaxPro使用的基本設定。

大家也可以在這個基礎上加強,寫得更通用一些:)

繼續閱讀