[RESOLVED] Lock disable controls.

After user logs in, if user is of a particular role (I am using standard asp membership provider), I want to show the user a data entry form completely with locked/disabled controls (Readonly). User should be able to see the data, but not be able to edit any single control.

I have few individual web controls and data controls in the form. How can I do this. 

 

Hi use

Roles.GetRolesForUser()

if it equal to perticular role then set all controls ReadOnly property equals to true.

 

EzzBoy

I want to show the user a data entry form completely with locked/disabled controls (Readonly). User should be able to see the data, but not be able to edit any single control.

If all the input controls are in a panel, you can just disable that depending on user role.

ps: You can also disable all input controls by looping page controls..

foreach (Control c in page.Controls)
	{
		if (c is TextBox
		|| c is DropDownList
		|| c is RadioButton)

		{
			(c as WebControl).Enabled = false;
		}
                }

Looks like a good solution, but I am not getting it to work..

This is the lay out structure I have.. I want to disable all controls from DivMain and downwards... DIV1, DIV2 ... etc.

<asp:Content id = "......>

     <asp:UpdatePanel id=".....>

        <GridView.....>

    </UpdatePanel>

    <DIVmain....>

           <DIV1.....>

               <UpdatePanel.....>

               </UpdatePanel>

            </DIV1>

 

           <DIV2.....>

               <UpdatePanel.....>

               </UpdatePanel>

            </DIV2>

 

          <DIV3.....>

               <UpdatePanel.....>

               </UpdatePanel>

            </DIV3>

    </DIVMain>

 </asp:Content>

 

EzzBoy

This is the lay out structure I have.. I want to disable all controls from DivMain and downwards... DIV1, DIV2 ... etc.

If all the controls are in UpdatePanel, have to disable like this..

foreach (Control c in UpdatePanel1.Controls)
            {
                if (c is TextBox
                || c is DropDownList
                || c is RadioButton)
                {
                    (c as WebControl).Enabled = false;
                }
            }
          // for controls in Updatepanel2
            foreach (Control c in UpdatePanel2.Controls)
            {
                if (c is TextBox
                || c is DropDownList
                || c is RadioButton)
                {
                    (c as WebControl).Enabled = false;
                }
            }
         
//Similarly for controls in other panels

Not working. 

After the update panel, there is the below structure... I put break point to see what is happening, the control c in the very first pass, is of UniqueId = IDctl00$MainContent$ctl01 .. it is not of any type of text box, dropdow, checbox, radio etc... for which I am checking... If I take the condition for checking type of control away and make it go to the c as webcontrol.enabled = false line of code, I get a Nullreferenceexception error..

Object reference not set to instance of an object message.... and c appears as null... something that I am doing wrong here.. thanks for your help...

 

 

<UpdatePanel>
    <ContentTemplate>

          <FieldSet> ..

                <FormView>....

         </FieldSet>

    </ContentTemplate>

</UpdatePanel>

 

 

This work..?

foreach (Control c in UpdatePanel1.Controls)
            {
                foreach (Control ctrl in c.Controls)
                {
                    if (ctrl is TextBox)

                        ((TextBox)ctrl).Enabled = false;

                    else if (ctrl is RadioButton)

                        ((RadioButton)ctrl).Enabled = false;

                    else if (ctrl is CheckBox)

                        ((CheckBox)ctrl).Enabled = status;

                    else if (ctrl is DropDownList)

                        ((DropDownList)ctrl).Enabled = status;
                }
            }

Thank you for your help. That is the solution.

thoughts on "[RESOLVED] Lock disable controls."

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>