dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: [RESOLVED] Enable/Disable asp.net radio by javascript

  1. #1
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    [RESOLVED] Enable/Disable asp.net radio by javascript

    Hi frends !
    I must confess this is my second day to use any javy script code and it confuses me a bit as it seems a bit similar to c# but is totally different in lots of points.
    For example I'm missing declaration of input values in methods. My actual problem is to find the needed access to an asp.net radiobutton control using clientside code
    I did for a RadioButton where the ID is "radio1"
    Code:
    
    var tbx = document.getElementById("radio1");
    tbx.disabled = true;
    This disabled /enabled the button itself depending on if I set disabled to true or false but didn't disable the text of the control only the button.
    Using Postback and Enabled Property of this asp.net Radiobutton I can easily enabe /disable the whole control within its text but not using this code in javascript. So how to get access to the whole control ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Enable/Disable asp.net radio by javascript

    You would have to also disable the textbox by getting its ID.

    Code:
    document.getElementById('RADIO_ID').disabled = true;
    document.getElementById('TEXTBOX_ID').disabled = true;
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Enable/Disable asp.net radio by javascript

    you may use the control's ClientID property when in client-side..

    document.getElementById('<%=myTextBox.ClientID%>').disabled = true;

    hth
    Busy

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Enable/Disable asp.net radio by javascript

    Quote Originally Posted by PeejAvery View Post
    You would have to also disable the textbox by getting its ID.

    Code:
    document.getElementById('RADIO_ID').disabled = true;
    document.getElementById('TEXTBOX_ID').disabled = true;
    Yes, Ok, but my problem is which ID has the Text of an asp.net Radiobutton ? The whole control has an ID "Radio1"... and so on. So whats the ID of its Text ? Is the Text a child of the radio Button ? The design is a list the user can fill and depending on some checkboxes some Radiobuttons needs to be enabled / disabled. And all this should be done on clientside.

    Or do I need to have the Radiobuttons Textfields empty and adding additional Labels and changing TextColor there ? Would be really a complicated solution for that
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Enable/Disable asp.net radio by javascript

    Quote Originally Posted by Thread1 View Post
    you may use the control's ClientID property when in client-side..

    document.getElementById('<%=myTextBox.ClientID%>').disabled = true;

    hth
    Sorry I'm too much new I dont understand what you are talking about. Lets assume This are my Radios
    Code:
    <scriptlanguage="javascript">
    function ChangeRadioState(which,disable){
    var tbx = document.getElementById(which);
    tbx.disabled = disable;
    }
     
    </script>
    <body>
    <formid="form1"runat="server">
    <div>
    <asp:RadioButtonID="Radio1"runat="server"Text="Hallo"/><br/>
    <asp:RadioButtonID="Radio2"runat="server"Text="World"/><br/>
    <asp:RadioButtonID="Radio3"runat="server"Text="I'm here"/><br/> 
    <asp:CheckBoxID="CheckBox1"runat="server"Text="Disable Radio1"onclick="ChangeRadioState('Radio1',true)"/><br/>
    <asp:CheckBoxID="CheckBox2"runat="server"Text="Disable Radio1"onclick="ChangeRadioState('Radio1',false)"/></div>
    </form>
    </body>
    
    And when I'm disabling "Radio1" then also the Text which is "Hallo" in this example should look disabled. Just if I'm disabling it using Postback and setting the property of asp.net Control "Radio1" to Enabled="False"

    What is 'myTextBox' in your code in this case ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Enable/Disable asp.net radio by javascript

    the ID of the element in your aspx file is not necessarily the ID in javascript, but rather the ID of your server control. ASP.NET assigns its own ID for every element in the page, and this ID which is stored in ClientID property is the one used when the page is rendered to the client (javascript). you can verify this by looking at the "View Source" output of your browser.


    "myTextBox" is an ID of a textbox control which is similar to "Radio1" in your example, if you want to disable "Radio1" in javascript it could be

    Code:
    document.getElementById("<%=Radion1.ClientID%>").disabled = true;
    hth
    Busy

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Enable/Disable asp.net radio by javascript

    Hi friends !
    Using Debugger and Intellisense I found out that nextSibling seems to be the Text of the control So I did
    Code:
    
    <scriptlanguage="javascript">
    function ChangeRadioState(which,disable){
    var tbx = document.getElementById(which);
    tbx.disabled = disable;
    tbx.nextSibling.disabled = disable;
    }
    </script>
    and this seems to work. My question now is only, as I'm not familar with the asp.net controls design. Is this the standard way to do it or does this only work because of a 'lucky coincidental ' and will maybe not work if others access my page ? Or could it lead to this ugly errors which occur on lots of pages where my browser all the time asks if he should go to debug mode. Because I dont want to create lots of bugs on my page
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Enable/Disable asp.net radio by javascript

    and this seems to work. My question now is only, as I'm not familar with the asp.net controls design. Is this the standard way to do it or does this only work because of a 'lucky coincidental ' and will maybe not work if others access my page ? Or could it lead to this ugly errors which occur on lots of pages where my browser all the time asks if he should go to debug mode. Because I dont want to create lots of bugs on my page
    i think you should test it for different browser (i.e., firefox, opera, etc) though for i had encounter such thing that just because of an extra whitespace the "nextSibling" was affected.
    Busy

  9. #9
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Enable/Disable asp.net radio by javascript

    to avoid any problems I'd recommend using el.disabled="disabled" variant in js. This one works in all browsers.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that Iím 64-bit and my blanket is 32-bit and I couldnít cover myself with it, so Iíve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Enable/Disable asp.net radio by javascript

    Quote Originally Posted by Xeel View Post
    to avoid any problems I'd recommend using el.disabled="disabled" variant in js. This one works in all browsers.
    And what is el in your code ? ( which I sined in red )
    As you see I have a lot of radio buttons to enable / disable so I want to access them all with one method only using different input values. Look at the example and code I gave. I didn't want to explicitly write
    Code:
    Radio1.disabled= true;
    Radio2.disabled = true;
    ....
    Radio27.disabled = true;
    The full page is a relatively long questionary and depending on some answers further questions should be enabled or if they are not relevant anymore because of a given result in a former checkbox they should be disabled. For example if someone has answered he is a man its unnecessary to ask him if he ever has been pregnant and if yes how often. This is only an example, the questionary has a totally technical background ) But its simple a sort of multichoice.
    I would hate it to have a postback after each click a user does to any checkbox or radiobutton.
    Last edited by JonnyPoet; December 16th, 2008 at 02:30 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  11. #11
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Enable/Disable asp.net radio by javascript

    el is the common reference to an HTML element. In this case, it would be the object returned by document.getElementById().
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Enable/Disable asp.net radio by javascript

    Quote Originally Posted by Xeel View Post
    to avoid any problems I'd recommend using el.disabled="disabled" variant in js. This one works in all browsers.
    If I'm using this 'string' version how do I enable the button again ? I tried el.disabled="enabled" but this didn't work, while true and false works in my IE7 Browser. You say using sting works in every browser so I would use this but how to correctly set it to enabled ( using an empty string like "" works but is this the correct method. Sorry I'm a totally newbie in javascript and some of this javascript syntax is really against all what I have learned in C# about type and typesave coding. So as I can see el in my Intellisence it obviously doesn't need to be declared. So I changed my code as followed
    Code:
    function ChangeGroupState(gpCount, gpElement, disable) {
        for (i = 1; i <= gpCount; i++) {
             el = document.getElementById(gpElement + i);
             if (el !=null){
                if (disable) {
                    el.disabled = "disabled";
                    el.checked = false;
                    el.nextSibling.disabled = "disabled";
                } else {
                    el.disabled = "";
                    el.nextSibling.disabled = "";
                }
            }
        }
    }
    The radios have names like 'roof1' to 'roof4' or 'pos1' to 'pos6' and so on. Therefore gpElement is the basename like 'roof' or 'pos' and this way I can enable or disable a full bunch of radiobuttons. The last question regarding this is now only: Will this work in every browser or at least most of the browsers ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  13. #13
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Enable/Disable asp.net radio by javascript

    It should work in all browsers. However, it is a HTML value and not an XHTML value.

    Of course, there is no reason why the boolean shouldn't either. The boolean is the XHTML value.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)