可以先看看效果,被禁用的DropDownList的Items已经无法选取与颜色也变为灰色。
刚开始,DropDownList没有接收从网址传来的参数时,它每一个Item都是可以选取的,一旦接收了值。所对应的DropDownList的item将无法选取而达到禁用。
xxx.aspx:
<asp:DropDownList ID&#61;"DropDownList1" runat&#61;"server">
asp:DropDownList>
asp:DropDownList>
xxx.aspx.cs:
View Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
if (Request.QueryString["site"] !&#61; null)
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Text &#61;&#61; Request.QueryString["site"].ToString())
{
li.Attributes.Add("disabled", "disabled");
}
}
}
}
private void Data_Binding()
{
this.DropDownList1.DataSource &#61; Site();
this.DropDownList1.DataTextField &#61; "key";
this.DropDownList1.DataValueField &#61; "value";
this.DropDownList1.DataBind();
}
private Dictionary<string, string> Site()
{
Dictionary<string, string> site &#61; new Dictionary<string, string>();
site.Add("Insus.NET cnblogs", "http://insus.cnblogs.com");
site.Add("Microsoft", "http://www.microsoft.com");
site.Add("Google", "http://www.google.com");
return site;
}
{
if (!IsPostBack)
{
Data_Binding();
}
if (Request.QueryString["site"] !&#61; null)
{
foreach (ListItem li in DropDownList1.Items)
{
if (li.Text &#61;&#61; Request.QueryString["site"].ToString())
{
li.Attributes.Add("disabled", "disabled");
}
}
}
}
private void Data_Binding()
{
this.DropDownList1.DataSource &#61; Site();
this.DropDownList1.DataTextField &#61; "key";
this.DropDownList1.DataValueField &#61; "value";
this.DropDownList1.DataBind();
}
private Dictionary<string, string> Site()
{
Dictionary<string, string> site &#61; new Dictionary<string, string>();
site.Add("Insus.NET cnblogs", "http://insus.cnblogs.com");
site.Add("Microsoft", "http://www.microsoft.com");
site.Add("Google", "http://www.google.com");
return site;
}