Make a control Readonly using the controls Collection

Discussion in 'ASP .Net' started by =?Utf-8?B?R1A=?=, Sep 28, 2004.

  1. Is it possible to iterate through all the controls collection and make the
    textboxes alone as read only.I don't see a readonly property for the
    Control.Can some one help me in this context?
    I want to do something like this below.But I get a message Readonly is not
    valid property.
    foreach (Control ctl in pnlBenefits.Controls)
    {
    if (ctl is TextBox)
    ctl.ReadOnly=false;
    }
     
    =?Utf-8?B?R1A=?=, Sep 28, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?R1A=?=

    Curt_C [MVP] Guest

    I believe what you are after is:
    ..GetType()


    --
    Curt Christianson
    Owner/Lead Developer, DF-Software
    Site: http://www.Darkfalz.com
    Blog: http://blog.Darkfalz.com


    "GP" <> wrote in message
    news:...
    > Is it possible to iterate through all the controls collection and make the
    > textboxes alone as read only.I don't see a readonly property for the
    > Control.Can some one help me in this context?
    > I want to do something like this below.But I get a message Readonly is not
    > valid property.
    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > if (ctl is TextBox)
    > ctl.ReadOnly=false;
    > }
    >
    >
    >
     
    Curt_C [MVP], Sep 28, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?R1A=?=

    Hermit Dave Guest

    why dont you do a explicit typecast and then get its readonly property
    foreach (Control ctl in pnlBenefits.Controls)
    {
    if (ctl is TextBox)
    {
    ((TextBox)ctl).ReadOnly = false;
    }
    }

    i think this should do the job
    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "GP" <> wrote in message
    news:...
    > Is it possible to iterate through all the controls collection and make the
    > textboxes alone as read only.I don't see a readonly property for the
    > Control.Can some one help me in this context?
    > I want to do something like this below.But I get a message Readonly is not
    > valid property.
    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > if (ctl is TextBox)
    > ctl.ReadOnly=false;
    > }
    >
    >
    >
     
    Hermit Dave, Sep 28, 2004
    #3
  4. Hi Hermit Dave,

    Thanks for you response.
    This time it didn't error me out, but none of my text boxes is set to
    readonly.
    My text boxes are declared using System.Web.UI.WebControls.TextBox,Is it
    considered of Type TextBox too while using the Controls iteration.

    Thanks
    GP


    "Hermit Dave" wrote:

    > why dont you do a explicit typecast and then get its readonly property
    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > if (ctl is TextBox)
    > {
    > ((TextBox)ctl).ReadOnly = false;
    > }
    > }
    >
    > i think this should do the job
    > --
    >
    > Regards,
    >
    > Hermit Dave
    > (http://hdave.blogspot.com)
    > "GP" <> wrote in message
    > news:...
    > > Is it possible to iterate through all the controls collection and make the
    > > textboxes alone as read only.I don't see a readonly property for the
    > > Control.Can some one help me in this context?
    > > I want to do something like this below.But I get a message Readonly is not
    > > valid property.
    > > foreach (Control ctl in pnlBenefits.Controls)
    > > {
    > > if (ctl is TextBox)
    > > ctl.ReadOnly=false;
    > > }
    > >
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?R1A=?=, Sep 28, 2004
    #4
  5. "GP" <> wrote in message
    news:...
    > Is it possible to iterate through all the controls collection and make the
    > textboxes alone as read only.I don't see a readonly property for the
    > Control.Can some one help me in this context?
    > I want to do something like this below.But I get a message Readonly is not
    > valid property.

    foreach (Control ctl in pnlBenefits.Controls)
    {
    if (ctl is TextBox)
    {
    TextBox txt = (TextBox) ctl;
    txt.ReadOnly=false;
    }
    }


    -----
    John Saunders
     
    John Saunders, Sep 28, 2004
    #5
  6. =?Utf-8?B?R1A=?=

    Hermit Dave Guest

    Yeap unless you have another control with the name TextBox it should map to
    System.Web.UI.WebControls.TextBox

    Get into Debug mode. put in a break point and see if its getting executed.
    also try setting Enabled property to false.

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "GP" <> wrote in message
    news:...
    > Hi Hermit Dave,
    >
    > Thanks for you response.
    > This time it didn't error me out, but none of my text boxes is set to
    > readonly.
    > My text boxes are declared using System.Web.UI.WebControls.TextBox,Is it
    > considered of Type TextBox too while using the Controls iteration.
    >
    > Thanks
    > GP
    >
    >
    > "Hermit Dave" wrote:
    >
    > > why dont you do a explicit typecast and then get its readonly property
    > > foreach (Control ctl in pnlBenefits.Controls)
    > > {
    > > if (ctl is TextBox)
    > > {
    > > ((TextBox)ctl).ReadOnly = false;
    > > }
    > > }
    > >
    > > i think this should do the job
    > > --
    > >
    > > Regards,
    > >
    > > Hermit Dave
    > > (http://hdave.blogspot.com)
    > > "GP" <> wrote in message
    > > news:...
    > > > Is it possible to iterate through all the controls collection and make

    the
    > > > textboxes alone as read only.I don't see a readonly property for the
    > > > Control.Can some one help me in this context?
    > > > I want to do something like this below.But I get a message Readonly is

    not
    > > > valid property.
    > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > {
    > > > if (ctl is TextBox)
    > > > ctl.ReadOnly=false;
    > > > }
    > > >
    > > >
    > > >

    > >
    > >
    > >
     
    Hermit Dave, Sep 28, 2004
    #6
  7. Thanks for your responses.
    When I set to readonly it didn't work.Still I am able to edit the controls.
    But when I set the control enabled=false.I see the text boxes disabled.
    But I want the text box to be read only,so that I can get the values in the
    viewstate.
    foreach (Control ctl in pnlBenefits.Controls)
    {
    s= ctl.GetType().Name;
    if (s == "TextBox")
    {
    ((TextBox)ctl).ReadOnly = false;
    }
    }
    }
    Even I tried TextBox txt = (TextBox) ctl;
    txt.ReadOnly=false;

    Thanks
    GP

    "Hermit Dave" wrote:

    > Yeap unless you have another control with the name TextBox it should map to
    > System.Web.UI.WebControls.TextBox
    >
    > Get into Debug mode. put in a break point and see if its getting executed.
    > also try setting Enabled property to false.
    >
    > --
    >
    > Regards,
    >
    > Hermit Dave
    > (http://hdave.blogspot.com)
    > "GP" <> wrote in message
    > news:...
    > > Hi Hermit Dave,
    > >
    > > Thanks for you response.
    > > This time it didn't error me out, but none of my text boxes is set to
    > > readonly.
    > > My text boxes are declared using System.Web.UI.WebControls.TextBox,Is it
    > > considered of Type TextBox too while using the Controls iteration.
    > >
    > > Thanks
    > > GP
    > >
    > >
    > > "Hermit Dave" wrote:
    > >
    > > > why dont you do a explicit typecast and then get its readonly property
    > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > {
    > > > if (ctl is TextBox)
    > > > {
    > > > ((TextBox)ctl).ReadOnly = false;
    > > > }
    > > > }
    > > >
    > > > i think this should do the job
    > > > --
    > > >
    > > > Regards,
    > > >
    > > > Hermit Dave
    > > > (http://hdave.blogspot.com)
    > > > "GP" <> wrote in message
    > > > news:...
    > > > > Is it possible to iterate through all the controls collection and make

    > the
    > > > > textboxes alone as read only.I don't see a readonly property for the
    > > > > Control.Can some one help me in this context?
    > > > > I want to do something like this below.But I get a message Readonly is

    > not
    > > > > valid property.
    > > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > > {
    > > > > if (ctl is TextBox)
    > > > > ctl.ReadOnly=false;
    > > > > }
    > > > >
    > > > >
    > > > >
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?R1A=?=, Sep 28, 2004
    #7
  8. =?Utf-8?B?R1A=?=

    Hermit Dave Guest

    i think it could be based on rendering of the textbox. at the end of the day
    its the browser that acts on the html attribute in the element and makes it
    readonly.
    with enabled = false i think you cannot do anything with the instance. ie no
    modifications cant even get the focus. you still have access to the object
    and the viewstate etc from serverside. The enabled = false is only for
    client side rendering.

    --

    Regards,

    Hermit Dave
    (http://hdave.blogspot.com)
    "GP" <> wrote in message
    news:...
    > Thanks for your responses.
    > When I set to readonly it didn't work.Still I am able to edit the

    controls.
    > But when I set the control enabled=false.I see the text boxes disabled.
    > But I want the text box to be read only,so that I can get the values in

    the
    > viewstate.
    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > s= ctl.GetType().Name;
    > if (s == "TextBox")
    > {
    > ((TextBox)ctl).ReadOnly = false;
    > }
    > }
    > }
    > Even I tried TextBox txt = (TextBox) ctl;
    > txt.ReadOnly=false;
    >
    > Thanks
    > GP
    >
    > "Hermit Dave" wrote:
    >
    > > Yeap unless you have another control with the name TextBox it should map

    to
    > > System.Web.UI.WebControls.TextBox
    > >
    > > Get into Debug mode. put in a break point and see if its getting

    executed.
    > > also try setting Enabled property to false.
    > >
    > > --
    > >
    > > Regards,
    > >
    > > Hermit Dave
    > > (http://hdave.blogspot.com)
    > > "GP" <> wrote in message
    > > news:...
    > > > Hi Hermit Dave,
    > > >
    > > > Thanks for you response.
    > > > This time it didn't error me out, but none of my text boxes is set to
    > > > readonly.
    > > > My text boxes are declared using System.Web.UI.WebControls.TextBox,Is

    it
    > > > considered of Type TextBox too while using the Controls iteration.
    > > >
    > > > Thanks
    > > > GP
    > > >
    > > >
    > > > "Hermit Dave" wrote:
    > > >
    > > > > why dont you do a explicit typecast and then get its readonly

    property
    > > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > > {
    > > > > if (ctl is TextBox)
    > > > > {
    > > > > ((TextBox)ctl).ReadOnly = false;
    > > > > }
    > > > > }
    > > > >
    > > > > i think this should do the job
    > > > > --
    > > > >
    > > > > Regards,
    > > > >
    > > > > Hermit Dave
    > > > > (http://hdave.blogspot.com)
    > > > > "GP" <> wrote in message
    > > > > news:...
    > > > > > Is it possible to iterate through all the controls collection and

    make
    > > the
    > > > > > textboxes alone as read only.I don't see a readonly property for

    the
    > > > > > Control.Can some one help me in this context?
    > > > > > I want to do something like this below.But I get a message

    Readonly is
    > > not
    > > > > > valid property.
    > > > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > > > {
    > > > > > if (ctl is TextBox)
    > > > > > ctl.ReadOnly=false;
    > > > > > }
    > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >
     
    Hermit Dave, Sep 28, 2004
    #8
  9. Thanks for you answer
    "Hermit Dave" wrote:

    > i think it could be based on rendering of the textbox. at the end of the day
    > its the browser that acts on the html attribute in the element and makes it
    > readonly.
    > with enabled = false i think you cannot do anything with the instance. ie no
    > modifications cant even get the focus. you still have access to the object
    > and the viewstate etc from serverside. The enabled = false is only for
    > client side rendering.
    >
    > --
    >
    > Regards,
    >
    > Hermit Dave
    > (http://hdave.blogspot.com)
    > "GP" <> wrote in message
    > news:...
    > > Thanks for your responses.
    > > When I set to readonly it didn't work.Still I am able to edit the

    > controls.
    > > But when I set the control enabled=false.I see the text boxes disabled.
    > > But I want the text box to be read only,so that I can get the values in

    > the
    > > viewstate.
    > > foreach (Control ctl in pnlBenefits.Controls)
    > > {
    > > s= ctl.GetType().Name;
    > > if (s == "TextBox")
    > > {
    > > ((TextBox)ctl).ReadOnly = false;
    > > }
    > > }
    > > }
    > > Even I tried TextBox txt = (TextBox) ctl;
    > > txt.ReadOnly=false;
    > >
    > > Thanks
    > > GP
    > >
    > > "Hermit Dave" wrote:
    > >
    > > > Yeap unless you have another control with the name TextBox it should map

    > to
    > > > System.Web.UI.WebControls.TextBox
    > > >
    > > > Get into Debug mode. put in a break point and see if its getting

    > executed.
    > > > also try setting Enabled property to false.
    > > >
    > > > --
    > > >
    > > > Regards,
    > > >
    > > > Hermit Dave
    > > > (http://hdave.blogspot.com)
    > > > "GP" <> wrote in message
    > > > news:...
    > > > > Hi Hermit Dave,
    > > > >
    > > > > Thanks for you response.
    > > > > This time it didn't error me out, but none of my text boxes is set to
    > > > > readonly.
    > > > > My text boxes are declared using System.Web.UI.WebControls.TextBox,Is

    > it
    > > > > considered of Type TextBox too while using the Controls iteration.
    > > > >
    > > > > Thanks
    > > > > GP
    > > > >
    > > > >
    > > > > "Hermit Dave" wrote:
    > > > >
    > > > > > why dont you do a explicit typecast and then get its readonly

    > property
    > > > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > > > {
    > > > > > if (ctl is TextBox)
    > > > > > {
    > > > > > ((TextBox)ctl).ReadOnly = false;
    > > > > > }
    > > > > > }
    > > > > >
    > > > > > i think this should do the job
    > > > > > --
    > > > > >
    > > > > > Regards,
    > > > > >
    > > > > > Hermit Dave
    > > > > > (http://hdave.blogspot.com)
    > > > > > "GP" <> wrote in message
    > > > > > news:...
    > > > > > > Is it possible to iterate through all the controls collection and

    > make
    > > > the
    > > > > > > textboxes alone as read only.I don't see a readonly property for

    > the
    > > > > > > Control.Can some one help me in this context?
    > > > > > > I want to do something like this below.But I get a message

    > Readonly is
    > > > not
    > > > > > > valid property.
    > > > > > > foreach (Control ctl in pnlBenefits.Controls)
    > > > > > > {
    > > > > > > if (ctl is TextBox)
    > > > > > > ctl.ReadOnly=false;
    > > > > > > }
    > > > > > >
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?R1A=?=, Sep 29, 2004
    #9
  10. =?Utf-8?B?R1A=?=

    Matt Berther Guest

    Hello John,

    This may work too...

    foreach (TextBox tb in pnlBenefits.Controls)
    {
    tb.ReadOnly = false;
    }

    --
    Matt Berther
    http://www.mattberther.com

    > "GP" <> wrote in message
    > news:...
    >
    >> Is it possible to iterate through all the controls collection and
    >> make the
    >> textboxes alone as read only.I don't see a readonly property for the
    >> Control.Can some one help me in this context?
    >> I want to do something like this below.But I get a message Readonly
    >> is not
    >> valid property.

    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > if (ctl is TextBox)
    > {
    > TextBox txt = (TextBox) ctl;
    > txt.ReadOnly=false;
    > }
    > }
    > -----
    > John Saunders
     
    Matt Berther, Sep 29, 2004
    #10
  11. =?Utf-8?B?R1A=?=

    Tiësto Guest

    I've done it today with a client script. You can place this snippet at the
    botton of the page or within a funcion you call when you want. You can write
    it in the aspx file or render it exactly where you want.

    this is a sample, hope it helps:

    <script language="javascript">
    <!--
    var inputs = document.getElementsByTagName("INPUT");
    var selects = document.getElementsByTagName("SELECT");

    for (var i = 0;i<inputs.length;i++)
    {
    if ( (inputs.type == "text") && (inputs.disabled) )
    {

    inputs.disabled = false;
    inputs.readOnly = true;
    inputs.className = "Normal";
    inputs.tabIndex = -1;

    }
    }

    for (var i = 0;i<selects.length;i++)
    {
    selects.className = "Normal";
    selects.tabIndex = -1;
    selects[1].readOnly = true;
    }
    //-->
    </script>

    "GP" <> wrote in message
    news:...
    > Is it possible to iterate through all the controls collection and make the
    > textboxes alone as read only.I don't see a readonly property for the
    > Control.Can some one help me in this context?
    > I want to do something like this below.But I get a message Readonly is not
    > valid property.
    > foreach (Control ctl in pnlBenefits.Controls)
    > {
    > if (ctl is TextBox)
    > ctl.ReadOnly=false;
    > }
    >
    >
    >
     
    Tiësto, Nov 19, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
  2. Chad Scharf
    Replies:
    3
    Views:
    894
    Chad Scharf
    Oct 18, 2007
  3. adlionel

    .Net 2.0 : Smart tags and Readonly Collection Property

    adlionel, Nov 10, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    139
    adlionel
    Nov 10, 2005
  4. Jonathan Hyatt
    Replies:
    1
    Views:
    1,949
    Jonathan Hyatt
    Jun 8, 2004
  5. Greg  --

    Collection Readonly

    Greg --, Jul 25, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    113
    Greg --
    Jul 25, 2005
Loading...

Share This Page