程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net中在前台用js增刪ListBox的items

asp.net中在前台用js增刪ListBox的items

編輯:.NET實例教程


現在的項目中,有個搜索部分,需要對搜索結果Grid顯示字段讓用戶選擇

於是 用2個ListBox分別列示所有可顯示字段和已選擇顯示字段

並在前台用JS控制2個Listbox之間項目的增刪和移動
(沒辦法,測試mm說後台事件有刷新的看著不舒服...)

於是 搞2個Html button
事件如下:(先別管Hidden1)


function Test( lbFIEldsSelect,value)
            {
                for (j = 0;j<lbFIEldsSelect.length; j++)
                {                    
                    if (lbFIEldsSelect.options[j].value == value) return true;            
                }
                return false;
            }
            
            function SelectFIEld()
            {
                var lbFields = document.getElementById("lbFIElds");                
                var lbFieldsSelect = document.getElementById("lbFIEldsSelect");
                var Hidden1 = document.getElementById("Hidden1");            
                
                for (i = 0;i<lbFIElds.length; i++)
                {
                    if (lbFIElds.options[i].selected)
                    {                        
                        if  (Test(lbFieldsSelect,lbFIElds.options[i].value))
           {
                            //已經選擇該字段
                            continue;
                        }
                        
                        lbFIEldsSelect.options.add(document.createElement("OPTION"));
                        lbFieldsSelect.options[lbFieldsSelect.length-1].text=lbFIElds.options[i].text;
                        lbFieldsSelect.options[lbFieldsSelect.length-1].value=lbFIElds.options[i].value;
                        Hidden1.value = Hidden1.value + "#" + lbFields.options[i].text + "@" + lbFIElds.options[i].value;
                    }
                }
            }
            
            function UnSelectFIEld()
            {
                var lbFields = document.getElementById("lbFIElds");                
                var lbFieldsSelect = document.getElementById("lbFIEldsSelect");
                var Hidden1 = document.getElementById("Hidden1");
                
                for (i = 0;i<lbFIEldsSelect.length; i++)
           &
nbsp;    {
                    if (lbFIEldsSelect.options[i].selected)
                    {                    
                        lbFIEldsSelect.options.add(document.createElement("OPTION"));
                        lbFieldsSelect.options[lbFieldsSelect.length-1].text=lbFIElds.options[i].text;
                        lbFieldsSelect.options[lbFieldsSelect.length-1].value=lbFIElds.options[i].value;
                        Hidden1.value = Hidden1.value + "#" + lbFields.options[i].text + "@" + lbFIElds.options[i].value;
                    }
                }
                
            }
不料提交到後台時,前台增加的ListItem都沒有了
仔細想想,肯定是ASP.Net沒有在回發時加上ListBox客戶端改變後的VIEwState
其實也不能怪ms,連Table都沒有ViewState(按照ms的說法,table只是個容器,table裡面的控件才有必要維持VIEwState)
ListBox這種客戶端難得改變一次的東西顯然是更不需要了
沒辦法,只好自己弄一個可以維持vIEwstate的東西來放listBox的Item了
於是有了上面的Hidden1

服務器端相應代碼:


        private void RestorelbFIEldsSelect()
        {
            string[] sFIEld =Hidden1.Value.Split('#'); 
            
            //lstShowFIEld.Items.Clear();
            DataTable dtFIEld = new DataTable();
            dtFIEld.Columns.Add("sDisplayName","".GetType());
            dtField.Columns.Add("sFIEldName","".GetType());
            for (int i = 0 ; i < sFIEld.Length ; i ++)
            {
             if (sField[i] == null || sFIEld[i] =="") continue;
                DataRow row = dtFIEld.NewRow();
                row[0] = sFIEld[i].Split('@')[0];
                row[1] = sFIEld[i].Split('@')[1];
                dtFIEld.Rows.Add(row);
            }
            
            lbFieldsSelect.DataSource = dtFIEld;
            lbFieldsSelect.DataTextFIEld = "sDisplayName";
            lbFieldsSelect.DataValueField = "sFIEldName";
            lbFIEldsSelect.DataBind();
        }
這樣的話,每次pageload時都根據hidden的狀態更新ListBox
我們項目的目標達到了(只需要ListBox的Item,不需要考慮選定等)
如果需要處理選定項,可能需要再多一個Hidden吧。

http://www.cnblogs.com/calmzeal/archive/2006/07/26/460068.Html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved