Showing posts with label Disable. Show all posts
Showing posts with label Disable. Show all posts

Friday, June 29, 2012

Asp.Net jQuery Restrict dropdown list Selection without disabling

In this post, We will see on how to restrict the user from selecting an option in a DropDownList without disabling the control, using jQuery.

In the post Disable DropDownList without blur effect in Asp.Net, we saw how to achieve this effect using JavaScript, but this required us to set the onfocus and onchange event functions in all the dropdowns.


onfocus="javascript:setDefaultIndex(this);"
onchange="javascript:setSelectedIndex(this);"

Here we will see on how to achieve the same effect using jQuery, believe me this is very easy compared to the JavaScript approach.

Just add the below script to the page, that’s it, the user cannot change the selected values in any of the DropDownList in the page.

<script type="text/javascript" src="JavaScript/jquery-1.7.2.js">script>
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        // Loop Through DropDowns
        $('select').each(function() {
            $(this).change(function(e) {
                $(this).prop("selectedIndex", $(this).prop("defaultIndex"));
            });
            $(this).focus(function(e) {
                $(this).prop("defaultIndex", $(this).prop("selectedIndex"));
            });
        });
    });
script>

We are capturing the focus and change events of all the DropDownList controls in the page. 
 - On focus we are getting the selectedIndex and setting it to defaultIndex
 - On change we are again getting the defaultIndex and setting it to the selectedIndex of the DropDownList controls 

Tuesday, June 26, 2012

Enable / Disable nested controls in an Asp.Net User Control


In the post Enable / Disable all controls in an Asp.Net Page, we saw how to Enable/Disable controls in an Asp.net page, there are situations in which we load User Controls in the main page, in these situations the normal approach will not enable/disable the nested/child controls in the User Control, since the user control will be treated as a single control in the asp.net page.

To Enable/Disable the child controls within the user control, we need to iterate through the controls in the user control and Enable/Disable them separately. Identify the control from the page using Page.FindControl(), and pass the control to the EnableUserControls() method.

ucMyControl objMyControl = (ucMyControl)Page.FindControl("ucMyControl");
if
 (strMode.ToUpper() == "VIEW"))
{
   EnableUserControls(false, objMyControl)
}
else if (strMode.ToUpper() == "EDIT"))
{
   
EnableUserControls(true, objMyControl)
}

Based on the mode VIEW/EDIT we will pass false/true to the EnableUserControls function, and the same will DISABLE/ENABLE controls in the page.

public void EnableUserControls(bool status, UserControl currControl)
{ 
  
foreach (Control ctrl in currControl.Controls)
   {
        if (ctrl is TextBox)
        ((TextBox)ctrl).ReadOnly = (ctrlStatus == true) ? false : true;

        else if (ctrl is Button)
         ((Button)ctrl).Enabled = ctrlStatus;

        else if (ctrl is RadioButtonList)
         ((RadioButtonList)ctrl).Enabled = ctrlStatus;

        else if (ctrl is ImageButton)
         ((ImageButton)ctrl).Enabled = ctrlStatus;

        else if (ctrl is CheckBox)
         ((CheckBox)ctrl).Enabled = ctrlStatus;

        else if (ctrl is DropDownList)
         ((DropDownList)ctrl).Enabled = ctrlStatus;

        else if (ctrl is HyperLink)
         ((HyperLink)ctrl).Enabled = ctrlStatus;

        else if (ctrl is HtmlImage)
         ((HtmlImage)ctrl).Visible = ctrlStatus;
   }
}

Notice the we are setting the .Enabled property for all other controls except the textBox control, when a TextBox is disbled the contents in the TextBox will not be clearly visible to the users hence we are setting the ReadOnly property for the TextBox, and this functions opposite to the Enable/Disable

If Enable is set to true then ReadOnly should be set to false
If Enable is set to false then ReadOnly should be set to true

To achieve this we are using a conditional operator to reverse the value for the ReadOnly property.

((TextBox)ctrl).ReadOnly = (ctrlStatus == true) ? false: true;

Related Post

Friday, April 27, 2012

Disable DropDownList without blur effect in Asp.Net

In View mode, we will set the .Enabled property of controls false, to prevent the users from modifying the values in the control, but this will create a blur effect on the controls and the text in the controls will not be visible clearly.  Especially in the DropDownList control, when the control is disabled the user will not be able to see the other options available in the DropDownList control.

We can overcome this with the help of some JavaScript, adding this script will enable the users to view the other options in the dropdown, but still makes sure that the user cannot change the currently selected option in the View mode.

Wednesday, April 25, 2012

Enable / Disable controls in an Asp.Net Panel


Enable / Disable controls in an Asp.Net Panel

In general for Asp.Net forms we maintain different modes like Create mode, View mode & Edit mode.

In the View mode we will disable all the controls to make sure that the user does not, modify any of the details. To disable the controls which are placed directly in the page, we can just iterate through the Page.Controls collection and Enable/Disable the controls. Refer the Post Enable / Disable all controls in an Asp.Net Page to see how to accomplish this.

However if there are Panel controls in the page then a mere iteration with the Page.Controls collection will not reflect the controls which are placed inside the Panel, to Enable/Disable controls in the Panel, we need to iterate throught the Controls in the Panel.

Here is the code to enable the controls in a Panel.

if (strMode.ToUpper() == "VIEW"))
{
    EnableControls(false);
}
else if (strMode.ToUpper() == "EDIT"))
{
    EnableControls(true);
}

Based on the mode VIEW/EDIT we will pass false/true to the EnableControls function, and the same will DISABLE/ENABLE controls in the Panel.
private void EnableControls(bool ctrlStatus)
{
 foreach (Control c in Page.Controls)
 {
  foreach (Control ctrl in c.Controls)
  {
    if (ctrl is Panel)
    {
foreach (Control plnCtrl in ctrl.Controls)
{
        if (plnCtrl is TextBox)
         ((TextBox) plnCtrl).ReadOnly = (ctrlStatus == true) ? false : true;

        else if (plnCtrl is Button)
         ((Button) plnCtrl).Enabled = ctrlStatus;

        else if (plnCtrl is RadioButtonList)
         ((RadioButtonList) plnCtrl).Enabled = ctrlStatus;

        else if (plnCtrl is ImageButton)
         ((ImageButton) plnCtrl).Enabled = ctrlStatus;

        else if (plnCtrl is CheckBox)
         ((CheckBox) plnCtrl).Enabled = ctrlStatus;

        else if (plnCtrl is DropDownList)
          ((DropDownList) plnCtrl).Enabled = ctrlStatus;

         else if (plnCtrl is HyperLink)
          ((HyperLink) plnCtrl).Enabled = ctrlStatus;

         else if (plnCtrl is HtmlImage)
    ((HtmlImage) plnCtrl).Visible = ctrlStatus;
    }
  }
 }
}

Notice the we are setting the .Enabled property for all other controls except the textBox control, when a TextBox is disbled the contents in the TextBox will not be clearly visible to the users hence we are setting the ReadOnly property for the TextBox, and this functions opposite to the Enable/Disable

If Enable is set to true then ReadOnly should be set to false
If Enable is set to false then ReadOnly should be set to true

To achieve this we are using a conditional operator to reverse the value for the ReadOnly property.

((TextBox)ctrl).ReadOnly = (ctrlStatus == true) ? false : true;

That’s it we have Enabled/Disabled the controls of an Asp.net Panel by iterating through the collection.


Related Post

Enable / Disable all controls in an Asp.Net Page


Enable / Disable all controls in an Asp.Net Page

In general for Asp.Net forms we maintain different modes like Create mode, View mode & Edit mode.

In the View mode we will disable all the controls to make sure that the user does not, modify any of the details. If the form is small then we can manually set the .Enabled property to false for all the controls in the form to achieve the effect, however if we have a huge form with hundreds of controls then this becomes a tedious task to set the property for each of the controls, in these scenarios we can make use of the Page.Controls collection to loop throught all the controls in the page and Enable/Disable the controls.

Here is the code to enable all the controls in an Asp.net page.

if (strMode.ToUpper() == "VIEW"))
{
    EnableControls(false);
}
else if (strMode.ToUpper() == "EDIT"))
{
    EnableControls(true);
}

Based on the mode VIEW/EDIT we will pass false/true to the EnableControls function, and the same will DISABLE/ENABLE controls in the page.

private void EnableControls(bool ctrlStatus)
{
  foreach (Control c in Page.Controls)
   {
    foreach (Control ctrl in c.Controls)
    {
      if (ctrl is TextBox)
        ((TextBox)ctrl).ReadOnly = (ctrlStatus == true) ? false : true;

        else if (ctrl is Button)
         ((Button)ctrl).Enabled = ctrlStatus;

        else if (ctrl is RadioButtonList)
         ((RadioButtonList)ctrl).Enabled = ctrlStatus;

        else if (ctrl is ImageButton)
         ((ImageButton)ctrl).Enabled = ctrlStatus;

        else if (ctrl is CheckBox)
         ((CheckBox)ctrl).Enabled = ctrlStatus;

        else if (ctrl is DropDownList)
         ((DropDownList)ctrl).Enabled = ctrlStatus;

        else if (ctrl is HyperLink)
         ((HyperLink)ctrl).Enabled = ctrlStatus;

        else if (ctrl is HtmlImage)
   ((HtmlImage)ctrl).Visible = ctrlStatus;
}
   }
}


Notice the we are setting the .Enabled property for all other controls except the textBox control, when a TextBox is disbled the contents in the TextBox will not be clearly visible to the users hence we are setting the ReadOnly property for the TextBox, and this functions opposite to the Enable/Disable

If Enable is set to true then ReadOnly should be set to false
If Enable is set to false then ReadOnly should be set to true

To achieve this we are using a conditional operator to reverse the value for the ReadOnly property.

((TextBox)ctrl).ReadOnly = (ctrlStatus == true) ? false : true;

This logic will work fine for the controls which are placed directly in the form, if the form has a Panel, we need to add another iteration, which will iterate through the controls in the Panel to Enable/Disable the panel controls. Refer to the Post Enable / Disable controls in an Asp.Net Panel, to see how to achieve this. 

That’s it we have Enabled/Disabled the controls of an Asp.net page by iterating through the Page.Controls collection.



Related Post