I have a user control and it has a method which is executed when button on aspx page is clicked, I m passing two ids of the user control in the method, Now i want to get the values of the textbox in the user control but unfortunately it is not recognizing textbox I have coded :-
//Method to copy values from one control into another
public void copyInfo(Control ctrl1, Control ctrl2) {
List<string> vals = new List<string>();
foreach (Control c in ctrl1.Controls)
{
if (c is TextBox)
{
if (string.IsNullOrEmpty(((TextBox)c).Text)) { }
else {
//values from textbox
vals.Add(((TextBox)c).Text);
}
}
.............
..........
......
how can i get the textbox control and there values.
I would add a public property on your UserControl such as:
Then you need to cast your controls into their actual types rather than the generic
Control
class:Or the other way around depending on which your from and dest are in the copy.
Update to discuss dynamic controls
If your controls are being dynamically created and you can't find it on postback its probably because you haven't recreated the controls on postback. I have found this article series to be very interesting when trying to understand how to work with and process data from dynamic controls:
That link is to part three but its where the important part is which covers your scenario: