I am using two user controls in my web application. I want to read a Label text from a user control via another user control. How can i read it?
-
My account blocked on this site.Now i am not able to ask questions. Can you tell me what should i do to ask question? – Neeraj May 03 '12 at 07:08
3 Answers
You should refactor your code and not rely to content of some label on another UI control. Get that value the same way as you do in that User Control, or extract that functionality in another class to avoid code duplication, and call it from both places.
But, if you wont to stick with this existing code you should create Interface and capture all UserControls functionality that you wont to called from outside code (in your case : return label text). Then implement that interface in User Controls that must be called from outside, after that is all about finding control instances, you can do that by enumerating all Page child controls. Here is the example code of simple interface that defines that control must return some Label text, and a class that finds user control by name in control tree :
public interface IUserControl
{
string LabelText();
}
public class PageUserControls
{
private Page parentPage;
public PageUserControls(Page myParentPage)
{
this.parentPage = myParentPage;
}
private IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
public IUserControl GetControl(string controlName)
{
foreach (Control cnt in EnumerateControlsRecursive(this.parentPage))
{
if (cnt is IUserControl && (cnt as UserControl).AppRelativeVirtualPath.Contains(controlName))
return cnt as IUserControl;
}
return null;
}
}
then you have to implement that interface in user control that holds that Label :
public partial class WebUserControl1 : System.Web.UI.UserControl, IUserControl
{
public string LabelText()
{
return Label1.Text;
}
}
And finally use it from another User control :
PageUserControls puc = new PageUserControls(this.Page);
string txt1 = puc.GetControl("WebUserControl1.ascx").LabelText();
btw. method EnumerateControlsRecursive is adopted from SO answer to Finding all controls in an ASP.NET Panel?

- 1
- 1

- 20,445
- 6
- 75
- 102
-
My account blocked on this site.Now i am not able to ask questions. Can you tell me what should i do to ask question? – Neeraj May 03 '12 at 07:05
Take a look at this article on MSDN.
In a short, you can access other controls if you know the ID.

- 13,265
- 4
- 47
- 74
-
My account blocked on this site.Now i am not able to ask questions. Can you tell me what should i do to ask question? – Neeraj May 03 '12 at 07:05
use like this...
create one public property in the user control and call that property using user controls name where you want that value....

- 71
- 5
-
My account blocked on this site.Now i am not able to ask questions. Can you tell me what should i do to ask question? – Neeraj May 03 '12 at 07:04