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);
}
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 controls in an Asp.Net Panel
Enable / Disable nested controls in an Asp.Net User Control
Enable / Disable nested controls in an Asp.Net User Control
14 comments:
It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training in velachery | dot net training institute in velachery
Hello there! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Can you recommend any other blogs/websites/forums that deal with the same subjects? Thanks.
Click here:
angularjs6 Training in Chennai
Click here:
Microsoft azure training in chennai
Click here:
angularjs6 Training in Chennai
Click here:
angularjs Training in online
This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
Click here:
Microsoft azure training in annanagar
Click here:
Microsoft azure training in velarchery
Click here:
Microsoft azure training in sollinganallur
Click here:
Microsoft azure training in btm
Click here:
Microsoft azure training in rajajinagar
I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
Data Science course in Chennai | Data science course in bangalore
Data science course in pune | Data science online course
Data Science Interview questions and answers | Python course in Kalyan nagar
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
java training in chennai | java training in bangalore
java interview questions and answers | core java interview questions and answers
Enrich your blog post very useful
microsft azure certification training in chennai
Wonderful post. Thanks for taking time to share this information with us.
Azure Training in Chennai
Microsoft Azure Training in Chennai
R Training in Chennai
R Programming Training in Chennai
Data Science course in Chennai
Data Analytics Courses in Chennai
Robotics Process Automation Training in Chennai
Azure Training in Anna Nagar
Azure Training in T Nagar
Great blog, I was searching this for a while. Do post more like this.
DevOps Training in Chennai
AWS Training in Chennai
Data Science Training in Chennai
ccna Training in Chennai
Python Training in Chennai
Angularjs Training in Chennai
RPA Training in Chennai
Blue Prism Training in Chennai
Such an great and nice blog, Thank you for sharing this wonderful post.
ExcelR Data Science Bangalore
Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
digital marketing
asp.net enable disable panel program is so useful. the effort you put to right is great. For microsoft azure training course in Chennaivisit Cognex technology.
Great information...Your post the very informative i have learned some information about your blog thank you for Sharing the great information.....
Best Furniture Manufacturers in South Delhi
Best Furniture Manufacturers in Delhi NCR
Good content shared, Thanks!
Data Science Training in Pune
Post a Comment