How do I tie a control to a JavaScript function

J

Jason

I'm new to WebForms and struggling with very basic stuff at the moment. I am
trying to figure out how to detect a radiobutten click state using
JavaScript.

The following OnCheckedChanged code fails at runtime:

<HTML>

<HEAD>

<title>WebForm1</title>

<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<meta content="C#" name="CODE_LANGUAGE">

<meta content="JavaScript" name="vs_defaultClientScript">

<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">

</HEAD>

<body>

<form id="Form1" method="post" runat="server">

<P>

<asp:RadioButton id="RadioButton1" runat="server" Checked="True"
Text="Purchase" GroupName="rb3"></asp:RadioButton><BR>

<asp:RadioButton id="RadioButton2" runat="server" Text="Refi"
GroupName="rb3" OnCheckedChanged="changed(this);"></asp:RadioButton></P>

<P>

</form>



<SCRIPT>

function changed(rb){

document.write("Click event");

}

</SCRIPT>

</P>

</body>

</HTML>
 
G

Girish Bharadwaj

OnCheckedChanged is a server side event. You need to implement the function
on the runat='server' codeblock.

You might want to look in the QuickStart tutorials. (usually installed when
you install .NET SDK).
 
G

Girish Bharadwaj

Use Click events with the radio (or simple Submit buttons) and then use
Radio.Checked flag to check which of the radio buttons is checked.
 
J

Joel Cade, MCSD

One easy way to accomplish this is adding to the attributes collection
of the control.

In you PageLoad, try adding

RadioButton1.Attributes.Add("onclick", "changed()")


Joel Cade, MCSD .Net, MCAD, MCP
Fig Tree Solutions, LLC
http://www.figtreesolutions.com
 
J

Jason

That did the trick. For the life of me, I couldn't find anything on how to
do this. The best I could come up with required an automatic postback where
the event could be handled in the code behind... that just didn't seem
effecient for my needs to make certain controls visible/hidden depending on
options selected.

Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top