Web-Forms – How a User-Control access another User-Control

I have two User-Controls and both are referenced in an .aspx page. How one User-Control can access the other User-Control, not from the code-behind?

For example?

Web-Forms - How a User-Control access another User-Control

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

IF you have 2 User Controls, you can access them by ID by going to the parent and using FindControl. In this example they are both the same UC, but that does not matter.

<uc1:WebUserControl1 runat="server" ID="WebUserControl1" />
<uc1:WebUserControl1 runat="server" ID="WebUserControl2" />

Then in code behind of WebUserControl1

//find the user control on the parent page
var usercontrol = Parent.FindControl("WebUserControl1") as UserControl;

//find the textbox in that usercontrol
var textbox = usercontrol.FindControl("TextBox1") as TextBox;

//do something with the textbox
textbox.Text = "Text in UserControl 1";


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x