Hello :)

i've created a user control that contains a label and button.
then i created a web page ... i put the User control in this page
how i could change the label text from the page?
i think i have to create a method? how and where i should create it.?
Thanks in advance!

Recommended Answers

All 4 Replies

In the code-behind for the .ascx (page were control exists) page, expose the property

VB Example...

Public Property lblControl As String
   Get
      Return lable1.Text.ToString
   End Get
End Property

C# Example...

public string lblControl 
{
    get 
    { 
        return label1.Text.ToString(); 
    }
}

Then, on your web page in the aspx page, you have to register the control and place the control on the page.

<%@ Register TagPrefix="uc" TagName="ctrl" Src="~/App_Controls/WebUserControl.ascx" %>

<uc:ctrl ID="ucCtrl" runat="server"  />

Then in the page's code behind...

VB

Dim test as String = ucCtrl.lblControl

C#

string test = ucCtrl.lblControl;

@JorgeM Thanks alot for your help and i have created Property then i take an object in my page load ..

    string test = ucCtrl.lblControl;

the question is.. how i could give the label a text after this step then?

Sorry, i guess i didnt read your question thorougly... I though you just wanted to get the value of the label control. if you also want to set the value, then modify the code-behind of the .ascx page, and add the set method. Just assign the label control's text property to the keyword value.

assuming C#

public string lblControl
{
     get
     {
          return Label1.Text.ToString();
     }

     set
     {
          Label1.Text = value;
     }
}

Then in your page's code behind...

ucCtrl.lblControl = "new value";

Solved , Thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.