Eric Cabeza
Eric Cabeza

Reputation: 43

how to access the controls in child user control from parent aspx page

I will like to know how I can access the textbox controls that I have in my WebUserControl from my Parent aspx page. I want that the user enters the data in texbox at the WebUserControl, then press the "Save" button which is in the parent page and the will be saved at SQL Server.

Upvotes: 4

Views: 9394

Answers (1)

Win
Win

Reputation: 62260

You can expose the Text of TextBox of WebUserControl as public.

WebUserControl.ascx.cs

public partial class WebUserControl : System.Web.UI.UserControl
{
    public string MyText
    {
        get { return MyTextBox.Text; }
        set { MyTextBox.Text = value; }
    }
}

WebForm1.aspx.cs

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        string value = WebUserControl1.MyText;
    }
}

Upvotes: 6

Related Questions