Disable or enable DropDownList Items

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.