Hello! Please help me solve 2 questions.

1) There is a page of the site where asp:Label . In the northern part, a reference is made to the class in which processing is performed. How to access the Text - Label'а property from this class?

2) There is a master page on which asp:Label is also located. How to change the Text property of this label from the server side of the child page?

Assumption: Use the object-oriented approach and do it through get, set . It just may be that both questions need to be taken into account, which leads to the need to refer from the class (to which I leave the child page) to the Label'а property, which is located on the master-странице . As a beginner in this field, it scares me. In addition, I hope that there are other ways to interact.

UPD:

I am trying to make a call to the element on the master-page from the child page.

Element:

 <asp:Label ID="Access_error" runat="server" Text="test53"></asp:Label> 

Appeal:

 Label lbl_TitlePage = Page.Master.FindControl("Access_error") as Label; lbl_TitlePage.Text = "THE KEY QUESTIONS"; 

I check through the debugger and see that lbl_TitlePage = null .

Although, if you look at the Master properties during debugging, then he sees Label perfectly

enter image description here

  • FindControl - only looks for controls that belong directly to this parent. You need to do this recursively. - Igor
  • @Igor Thank you for not leaving me alone with the trouble. I don’t figure out how to implement recursion in this case (I didn’t understand the past either. Why was the delegat taken. Why not get, set (even though you don’t even need get)) The implementation with the delegatom looks doubtful. Certainly, I would like to see an example. Well, or explanations of the delegate’s lines. I know I’m asking a lot. I haven’t gotten into such jungle yet. It would have been a good deal to get to the topic - Egor Trutnev

2 answers 2

Create a delegate property in your class that the class will use when it needs to access Label.Text without knowing what is going on inside the called delegate. The child page that creates the class object, let it assign (add) its method to this delegate, and in the method already refer to its or its parent Label .

  public delegate void SendString(string aValue); public class BusinessLogic { public SendString SendStringEvent; public void Execute() { string textToSend = "test"; if (SendStringEvent != null) SendStringEvent(textToSend ); } } class Program { static void SendStringEventHandler(string a) { Console.WriteLine(string.Format("*** {0} ***", a)); } static void Main() { BusinessLogic logic = new BusinessLogic(); logic.SendStringEvent += SendStringEventHandler; logic.Execute(); Console.ReadKey(); } } 

Update

 public static class ControlFinder { public static Control FindControlRecursive(this Control aParent, string anId) { Control result = aParent.FindControl(anId); if (result != null) return result; foreach (Control child in aParent.Controls) { result = child.FindControlRecursive(anId); if (result != null) return result; } return null; } } Label lbl_TitlePage = Page.Master.FindControlRecursive("Access_error") as Label; 
  • Can you give a simple example, please? With this, I have not yet come across. - Egor Trutnev
  • Thank you very much! At me, the truth, the debugger swore on FindControlRecursive and on return . And so, I found the answer to the second question myself. - Egor Trutnev
  • @Egor sorry, I missed the return type in the FindControlRecursive declaration. Added by. - Igor
  • There were really problems with return. But at the FindControlRecursive debugger continues to swear = ( i.stack.imgur.com/f7K9W.png - Egor Trutnev
  • @Egor move the ControlFinder class ControlFinder that it is directly in the namespace - Igor

Solution 2.

1) On the side of the master page:

 <asp:Label ID="Access_error" runat="server" Text="test53"></asp:Label> 

C #

 public string AccessError { set { Access_error.Text = value; } get { return Access_error.Text; } } 

2) On the side of the child page (pages with content):

 <%@ MasterType VirtualPath="~/Явная ссылка на master-страницу" %> 

WITH#

 this.Master.AccessError = "Текст.";