This article is an upgrade of http://www.bkjia.com/kf/201204/127918.html.
This is because netizens need to enable not only DropDownList Items, but also DropDownList. To prevent users from writing too much code. Insus. NET writes a category and inherits the System. web. UI. dropDownList in the WebControls namespace. you can see that the InsusDropDownList is instantiated and the DropDownList control is passed in. Then, the instantiated object can be DisableImsByText (), DisabletemsByVue (), EnableItemsBText (), and EnableItemsByValue () using the four methods ().
InsusDropDownList category:
InsusDropDownList
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI. WebControls;
/// <Summary>
/// Summary description for InsusDropDownList
/// </Summary>
Namespace Insus. NET
{
Public class InsusDropDownList: DropDownList
{
DropDownList _ DropDownList;
Public InsusDropDownList (DropDownList dropDownList)
{
This. _ DropDownList = dropDownList;
}
Public void DisableItemsByText (string text)
{
DisableItems (GetIndexByText (text ));
}
Public void EnableItemsByText (string text)
{
EnableItems (GetIndexByText (text ));
}
Public void DisableItemsByValue (string value)
{
DisableItems (GetIndexByValue (value ));
}
Public void EnableItemsByValue (string value)
{
EnableItems (GetIndexByValue (value ));
}
Private int GetIndexByText (string text)
{
Return this. _ DropDownList. Items. IndexOf (this. _ DropDownList. Items. FindByText (text ));
}
Private int GetIndexByValue (string value)
{
Return this. _ DropDownList. Items. IndexOf (this. _ DropDownList. Items. FindByValue (value ));
}
Private void DisableItems (int index)
{
If (index>-1)
This. _ DropDownList. Items [index]. Attributes. Add ("disabled", "disabled ");
}
Private void EnableItems (int index)
{
If (index>-1)
This. _ DropDownList. Items [index]. Attributes. Remove ("disabled ");
}
}
}
Demo, enable Items:
If (Request. QueryString ["site"]! = Null)
{
InsusDropDownList obj = new InsusDropDownList (this. DropDownList1 );
Obj. EnableItemsByText (Request. QueryString ["site"]);
}
From Insus. NET