2013-07-05 31 views
3

我正在使用ASP.NET,並且我動態地填充了我的DropDownList。 這裏是我的代碼:asp.net隱藏使用javascript的下拉列表中的具體項目

DataTable dtList = new DataTable(); 
     dtList.Columns.Add("Name"); 
     dtList.Columns.Add("Type"); 

foreach (DataDefinitionResponse dr in _dr) 
     { 
      if (dr.Type == "Dropdown") 
      { 
       string[] strSplit = dr.ListValue.Split('|'); 
       List<string> lst = new List<string>(); 

       foreach (string word in strSplit) 
       { 
        DataRow row = dtList.NewRow(); 
        row["Name"] = word; 
        row["Type"] = dr.Name; 
        dtList.Rows.Add(row); 
       } 
      } 
     } 

ddlFieldList.DataSource = dtList; 
     ddlFieldList.DataTextField = "Name"; 
     ddlFieldList.DataValueField = "Type"; 
     ddlFieldList.DataBind(); 

現在我只想使用Javascript選擇了其他的DropDownList時隱藏特定的項目。 我沒有在這裏使用SelectedIndexChanged。我必須使用Javascript。

請有人幫我這個。

THX

回答

0

我不認爲你可以用JavaScript來操縱的DropDownList和「擺脫它」,因爲當頁面隨後被回發到服務器ASP .NET將檢測到的DropDownList已被「篡改」並將拋出異常。

有一些可以設置的標誌來阻止錯誤,但是不太可能在代碼隱藏中使用DropDownList。

通常你會用SelectedIndexChanged(我知道你說你不想要)來實現你想要做的事情,並把控件放在UpdatePanel或類似的位置,以避免整頁的回傳/刷新。

0
function Remove() 
{ 
var DropDownList=document.getElementById('<%=DropDownList1.ClientID%>'); 
alert(DropDownList.value); 
for(i=DropDownList.length-1; i>=0; i--) 
{ 
    if(DropDownList.options[i].selected) 
    { 
     DropDownList.remove(i);   
    } 
} 
} 
相關問題