How do I tie a control to a JavaScript function

  • Thread starter Thread starter Jason
  • Start date Start date
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>
 
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).
 
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.
 
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
 
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.
 
Back
Top