asp.net使用javascript隐藏dropdownlist中的特定项目

时间:2013-07-05 10:11:51

标签: c# javascript asp.net dynamic

我正在使用ASP.NET,而且我正在动态填充我的DropDownList。 这是我的代码:

  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();

现在我只想在选择另一个DropDownList时使用Javascript隐藏特定项目。 我这里没有使用SelectedIndexChanged。我必须使用Javascript。

请有人帮助我。

THX

2 个答案:

答案 0 :(得分:0)

我认为你不能用JavaScript来操纵DropDownList,并且&#34;侥幸逃脱它&#34;因为当页面随后被发回服务器时,ASP .NET将检测到DropDownList已被篡改&#34;和将抛出异常。

您可以设置标志来停止错误,但您不太可能在代码隐藏中使用DropDownList。

您通常会使用SelectedIndexChanged(我知道您说过您并不想)来实现您的目标,并将控件置于UpdatePanel或类似内容以避免完整页面回发/刷新。

答案 1 :(得分: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);         
    }
}
}