Answered by:
Understanding the logic behind checkboxes inside a repeater control

Question
-
User2015884762 posted
Hi, I have got a repeater control of which one column is designated for checkboxes only. I was not able to get the checkbox ID to appear in the code behind until I found this code
CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("Checkbox1"); if (chk.Checked) { Label1.Text = "hello"; }
I understand that we are casting the checkbox within the repeater control to a checkbox object, but I would like to understand the logic behind it. I am pretty new to .Net and web forms, so would need to do some learning on the controls within another control. Please advice.
Wednesday, January 23, 2019 12:17 AM
Answers
-
User-2054057000 posted
In ASP.NET Web forms the checkbox, textbox, repeater are classes. You inorder to get an instance of a particular control. like repeater in your case, you have to find that control and cast it.
The code below is finding the 'Checkbox1' control by name. And then it is casted to 'Checkbox'.
Repeater1.Items[i].FindControl("Checkbox1")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 23, 2019 3:20 PM -
User839733648 posted
Hi callykalpana,
Just as yogyogi has explained, in webforms, the control like checkbox, textbox or the label are always the classes.
And when you want to find the specific one, you should use the function .FindControl to find it by its id.
For more about .FindControl, you could refer to the official document:
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.findcontrol?view=netframework-4.7.2
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 24, 2019 9:13 AM
All replies
-
User-2054057000 posted
In ASP.NET Web forms the checkbox, textbox, repeater are classes. You inorder to get an instance of a particular control. like repeater in your case, you have to find that control and cast it.
The code below is finding the 'Checkbox1' control by name. And then it is casted to 'Checkbox'.
Repeater1.Items[i].FindControl("Checkbox1")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 23, 2019 3:20 PM -
User839733648 posted
Hi callykalpana,
Just as yogyogi has explained, in webforms, the control like checkbox, textbox or the label are always the classes.
And when you want to find the specific one, you should use the function .FindControl to find it by its id.
For more about .FindControl, you could refer to the official document:
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.findcontrol?view=netframework-4.7.2
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 24, 2019 9:13 AM -
User2015884762 posted
Thanks for the guidance guys.
Friday, January 25, 2019 12:56 AM