New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Getting Started
The following tutorial demonstrates how to set up a page with a RadSwitch control and use its OnCheckedChanged server event:
-
In the default page of a new ASP.NET AJAX-enabled Web Application add a RadSwitch control:
ASP.NET<telerik:RadSwitch ID="RadSwitch1" runat="server"> </telerik:RadSwitch>
-
Set the
Text
of the RadSwitch Toggle states (unless you want them empty)ASP.NET<telerik:RadSwitch runat="server" ID="RadSwitch1" Value="0" CommandArgument="arg1"> <ToggleStates> <ToggleStateOff Text="No" /> <ToggleStateOn Text="Yes" /> </ToggleStates> </telerik:RadSwitch>
-
Add a Label to show a text associated with RadSwitch
ASP.NET<telerik:RadLabel ID="switchLabel" runat="server" AssociatedControlID="RadSwitch1" Text="I agree to the Terms & Conditions."></telerik:RadLabel>
-
To hook to the OnCheckedChanged server-side event of RadSwitch add an attribute to the main control tag and add the method signature:
ASP.NET<telerik:RadSwitch runat="server" ID="RadSwitch1" Value="0" CommandArgument="arg1" OnCheckedChanged="RadSwitch1_CheckedChanged"> <ToggleStates> <ToggleStateOff Text="No" /> <ToggleStateOn Text="Yes" /> </ToggleStates> </telerik:RadSwitch>
C#protected void RadSwitch1_CheckedChanged(object sender, EventArgs e) { }
VBProtected Sub RadSwitch1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) End Sub
-
Add a Label control to write the information to:
ASP.NET<telerik:RadLabel ID="Label1" runat="server"></telerik:RadLabel>
-
Use the CheckedChanged event handler to write information about the switch properties:
C#protected void RadSwitch1_CheckedChanged(object sender, EventArgs e) { var switchObj = sender as RadSwitch; var currentText = (bool)switchObj.Checked ? switchObj.ToggleStates.ToggleStateOn.Text : switchObj.ToggleStates.ToggleStateOff.Text; string data = string.Format("current text: {0}, current value {1}, current command argument: {2}, checked: {3}", currentText, switchObj.Value, switchObj.CommandArgument, switchObj.Checked); Label1.Text = data; }
VBProtected Sub RadSwitch1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Dim switchObj = TryCast(sender, RadSwitch) Dim currentText = If(CBool(switchObj.Checked), switchObj.ToggleStates.ToggleStateOn.Text, switchObj.ToggleStates.ToggleStateOff.Text) Dim data As String = String.Format("current text: {0}, current value {1}, current command argument: {2}, checked: {3}", currentText, switchObj.Value, switchObj.CommandArgument, switchObj.Checked) Label1.Text = data End Sub