[RESOLVED] Control radio using Script
Hi all,
I have one problem in ASP.NET 2.0. (I'm a beginner in ASP.Net. :bigyello: )
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!!!:D
Thanks.
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.
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 :D
Anyway, Thanks :)
Re: Control radio using Script
Use JavaScript.
From the codebehind,
vb Code:
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>
1 Attachment(s)
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 :bigyello: ) Thanks.
I think i still need to learn a lot. :p
N Thanks mendhak.
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?
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 :)