Results 1 to 7 of 7

Thread: [RESOLVED] Control radio using Script

  1. #1

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Resolved [RESOLVED] Control radio using Script

    Hi all,

    I have one problem in ASP.NET 2.0. (I'm a beginner in ASP.Net. )
    I want to control radio checked_changed event using VBScript.
    I already try this code in button click using script and its worked well but when I move to radio button, its not working.
    anybody know the reason and why???? (And I don't want to use 'AutoPostBack = true' because it post back to server every time i click on it. so the page seems to refresh every time i click. i don't want it to reload or refresh or flash the screen. just like windows based app to let it works smooth.)

    sorry to disturb you guys if my question is too simple.

    Here is my script code:
    Code:
     Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.TextBox1.Text = "Click on script"
            Me.TextBox1.Enabled = False
        End Sub
    And here is radio button code:
    Code:
    <asp:RadioButton ID="RadioButton1" runat="server" GroupName="a" Style="position: static" OnCheckedChanged="CheckedChanged" />
    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="a" Style="position: static" OnCheckedChanged="CheckedChanged"/>
    Please F1!!!

    Thanks.
    Last edited by scsfdev; Dec 9th, 2008 at 01:18 AM.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Control radio using Script

    Web Dev is not like Win Dev. Web Dev require you to set the AutoPostBack Property to TRUE.

    If you do not wish to refresh the page everytime you change the option, you can look into the AJAX way of accomplishing the same effect. Albeit, the page is still refreshed but to the end user it would look like the page didn't refresh at all.

    Or if your requirement is not too complex, the effect could be achieved using simple JS trick.

    Look around on google and here for numerous such examples.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Control radio using Script

    Quote Originally Posted by Harsh Gupta
    Web Dev is not like Win Dev. Web Dev require you to set the............Or if your requirement is not too complex, the effect could be achieved using simple JS trick.

    Look around on google and here for numerous such examples.

    Thanks Harsh Gupta!
    I also try googling but still no results for me (May be my english grammer skill is not good)
    and for AJAX, I think i still need more to get to that kind of level
    Anyway, Thanks
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Control radio using Script

    Use JavaScript.

    From the codebehind,

    vb Code:
    1. Me.RadioButton1.Attributes.Add("onclick","WriteToTxtBox('" & TextBox1.ClientID & "');")

    Then, on your page

    Code:
    <script type="text/Javascript">
    function WriteToTxtBox(txtID)
    {
    document.getElementById(txtID).value = 'click on script';
    document.getElementById(txtID).disabled = true;
    }
    </script>

  5. #5

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Control radio using Script

    Quote Originally Posted by mendhak
    Use JavaScript.

    From the codebehind,..........Then, on your page

    Code:
    <script type="text/Javascript">
    ........Id(txtID).disabled = true;
    }
    </script>

    Thanks mendhak.
    It working well with your code.
    But my problem is actually i want to hide a text box.
    I have something like:
    Leave Reason: Sick (Radio button)
    Personal (Radio button)
    Annual (Radio button)
    Other (Please Specify) (Radio button)
    (Here comes the textbox)

    So, my logic is <when user click on any first 3 radio buttons, textbox will invisible. if user choose Other, textbox will appear.>
    I try set value to document.getElementById(txtID).visible = false;
    Its not working. And so i tried googling and found this...
    document.getElementById(txtID).style.visibility='hidden';

    So, in anyway, i seems to manage the requirements. I attached my code with this post, can anyone please check whether it is in correct format of using code & script???? (Sorry for this, i know u r not corrector for me ) Thanks.

    I think i still need to learn a lot.

    N Thanks mendhak.
    Attached Files Attached Files
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Control radio using Script

    Yes, style.visibility will help you hide that textbox using client side scripting.

    No, your script won't work because RadioButton1 might have a different ID at runtime. So you'll want to pass that along to the function as well or make it a 'global' javascript variable. It'd be easier to show you the pass-it-along rather than confuse you with extra 'methods'.

    Me.RadioButton1.Attributes.Add("onclick", "ToggleTextBox('" & TextBox1.ClientID & "','" & RadioButton1.ClientID & "');")

    Code:
    function ToggleTextBox(txtID,rbID)
    {
        if(document.getElementById(rbID).checked==true)
        {
            document.getElementById(txtID).style.visibility='hidden';
        }
        else
        {
            document.getElementById(txtID).style.visibility='visible';
        }
    }
    Get the idea?

  7. #7

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: [RESOLVED] Control radio using Script

    Quote Originally Posted by mendhak
    Yes, style.visibility will help you hide that textbox using client side scripting.

    No, your script won't work because RadioButton1 might have a different ID at runtime. So you'll want to pass that along to the function as well or make it a 'global' javascript variable. It'd be easier to show you the pass-it-along rather than confuse you with extra 'methods'.

    Me.RadioButton1.Attributes.Add("onclick", "ToggleTextBox('" & TextBox1.ClientID & "','" & RadioButton1.ClientID & "');")

    Code:
    function ToggleTextBox(txtID,rbID)
    {
        if(document.getElementById(rbID).checked==true)
        {
            document.getElementById(txtID).style.visibility='hidden';
        }
        else
        {
            document.getElementById(txtID).style.visibility='visible';
        }
    }
    Get the idea?

    sorry mendhak i didn't see your post.
    Yes, I do pass both control clientID to script. In my example, i just make it simple.
    thanks for your reply
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

Posting Permissions

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



Click Here to Expand Forum to Full Width