zanhtet
zanhtet

Reputation: 2050

User Control In Asp.net

I created a user control. It contains one textbox. can I control this from parent web page.

Upvotes: 0

Views: 146

Answers (2)

marzouka
marzouka

Reputation: 384

Add the following property to your user control:

public string SomeValue
{
    get
    {
        return txtSample.Text;
    }
    set
    {
        txtSample.Text = value;
    }
}

And if you want to get or set the user control's textbox value from the page that contains the user control, just do the following:

MyUserControl.SomeValue = "Hello from page";
lblTest.Text = MyUserControl.SomeValue;

Where "MyUserControl" is the ID of the user control in the containing page.

Note: Since the TextBox control handles its Text property in the ViewState on its own, you don't have to explicitly handle it for this property.

Upvotes: 3

apros
apros

Reputation: 2878

Just define some public method or property in the user control and you can access it successfully. For example:

In UserControl1.ascx.cs:

public void DoSomething()
{
    //Do something here from UserControl
}

In the parent:

MyInstanceOfUserControl1.DoSomething();

Upvotes: 1

Related Questions