|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
A Brief IntroductionHave you ever wanted to disable/enable individual list items in a Instructions
ASP.NET CodeBehindThe following method adds the private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
LoadCheckBoxList();
}
}
private void LoadCheckBoxList()
{
this.checkBoxListTest.Attributes.Add("onclick",
"disableListItems('checkBoxListTest', '2', '3')");
// Add three items to the CheckBoxList.
for(int i=0; i < 3; i++)
{
ListItem item = new ListItem(i.ToString(), i.ToString());
this.checkBoxListTest.Items.Add(item);
}
}
The following code is the JavaScript function that is invoked by the JavaScript Function/*******************************************************************
This is the javascript function that is invoked when the checkboxlist
is clicked.
Function: disableListItems.
Inputs: checkBoxListId - The id of the checkbox list.
checkBoxIndex - The index of the checkbox to verify.
i.e If the 4th checkbox is clicked and
you want the other checkboxes to be
disabled the index would be 3.
numOfItems - The number of checkboxes in the list.
Purpose: Disables all the checkboxes when the checkbox to verify is
checked. The checkbox to verify is never disabled.
********************************************************************/
function disableListItems(checkBoxListId, checkBoxIndex, numOfItems)
{
// Get the checkboxlist object.
objCtrl = document.getElementById(checkBoxListId);
// Does the checkboxlist not exist?
if(objCtrl == null)
{
return;
}
var i = 0;
var objItem = null;
// Get the checkbox to verify.
var objItemChecked =
document.getElementById(checkBoxListId + '_' + checkBoxIndex);
// Does the individual checkbox exist?
if(objItemChecked == null)
{
return;
}
// Is the checkbox to verify checked?
var isChecked = objItemChecked.checked;
// Loop through the checkboxes in the list.
for(i = 0; i < numOfItems; i++)
{
objItem = document.getElementById(checkBoxListId + '_' + i);
if(objItem == null)
{
continue;
}
// If i does not equal the checkbox that is never to be disabled.
if(i != checkBoxIndex)
{
// Disable/Enable the checkbox.
objItem.disabled = isChecked;
// Should the checkbox be disabled?
if(isChecked)
{
// Uncheck the checkbox.
objItem.checked = false;
}
}
}
}
ConclusionThe above code shows that it is possible to enable/disable individual items within a
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||