This is a migrated thread and some comments may be shown as answers.

RadToggleSwitch how to get control name on ValueChanged event

5 Answers 261 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Memo
Top achievements
Rank 1
Iron
Veteran
Memo asked on 22 Jan 2021, 09:09 PM

I have toggle switches to mark a day of the week as closed.  The event sender is RadToggleSwitchElement and not RadToggleSwitch.  I need to get RadToggleSwitch.Name to extract the day of the week from the control's name which is then used down the line.

 

RadToggleSwitchElement.Name = null

RadToggleSwitchElement.Parent.Name = null 

 

How can I get the name of the RadToggleSwitch in the ValueChanged event handler?

 

TIA

5 Answers, 1 is accepted

Sort by
0
Memo
Top achievements
Rank 1
Iron
Veteran
answered on 23 Jan 2021, 11:13 PM

I've used a lambda expression for now.  I'm still wondering if there's a way to do it without and inside the handler.

1
Nadya | Tech Support Engineer
Telerik team
answered on 25 Jan 2021, 03:42 PM

Hello, Memo,

If I understand you correctly you use RadToggleSwitch controls. If the event sender is RadToggleSwitchElement is it possible for you to set the Name property of the RadToggleSwitchElement instead of RadToggleSwitch. Thus, you would be able to access their names in the ValueChanged event:

this.radToggleSwitch1.ToggleSwitchElement.Name = "Toggle1";
this.radToggleSwitch2.ToggleSwitchElement.Name = "Toggle2";
this.radToggleSwitch1.ToggleSwitchElement.ValueChanged += this.ToggleSwitchElement_ValueChanged;
this.radToggleSwitch2.ToggleSwitchElement.ValueChanged += this.ToggleSwitchElement_ValueChanged;

private void ToggleSwitchElement_ValueChanged(object sender, EventArgs e)
 {
     RadToggleSwitchElement switchElement = sender as RadToggleSwitchElement;
     Console.WriteLine(switchElement.Name);
 }

Another possible solution that I can suggest is to make use of the Tag property. It is of type object and you can store any useful information in it and use it when it is necessary. 

I hope this information helps. Let me know if I can assist you further.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Memo
Top achievements
Rank 1
Iron
Veteran
answered on 02 Feb 2021, 06:33 PM

Thank you for your reply, Nadya.  This does indeed help.

I am unable to set RadToggleSwitch.ToggleSwitchElement.Name in Element hierarchy editor  The change doesn't seem to be saved and the field has a blue square next to the field name whereas others don't (see attached).

So I have two additional questions if I may please:

1) Can the element name be set in the hierarchy editor or only set in code as in you example?

2) What are the blue squares found after some field names?

 

Thank again!
1
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 03 Feb 2021, 12:46 PM

Hello, Memo,

RadToggleSwitch.ToggleSwitchElement.Name should be set programmatically in the code. If you want to use the Element Hierarchy Editor you can set the Tag property as shown below for each RadToggleSwitchElement:

Then, use the ValueChanged event:

private void ToggleSwitchElement_ValueChanged(object sender, EventArgs e)
{
    RadToggleSwitchElement switchElement = sender as RadToggleSwitchElement;
    Console.WriteLine(switchElement.Tag);
}

The squares next to the fields indicated that the corresponding field has been modified. 

I hope this helps.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Memo
Top achievements
Rank 1
Iron
Veteran
answered on 11 Feb 2021, 04:51 PM
Brilliant!  Thank you very much, Nadya.
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Memo
Top achievements
Rank 1
Iron
Veteran
Answers by
Memo
Top achievements
Rank 1
Iron
Veteran
Nadya | Tech Support Engineer
Telerik team
Share this question
or