1

I hava a MasterPage with 2 UserControls. When something happens in UserControl1.ascx, it has to update a TextBox in UserControl2.ascx.

I tried this inside UserControl1.ascx, but no success:

UserControl userControl = (UserControl)LoadControl("UserControl2.ascx");
var txt = (TextBox) userControl.FindControl("txtTest");
txt.Text = "Hello world";

Thanks for all help :)

Øyvind Isaksen
  • 155
  • 3
  • 13

1 Answers1

0

The LoadControl is load a new control that is not exist on page...

Lets see this example. You have a master page that has 2 User Controls.

<form id="form1" runat="server">
    <div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <br /><br />
        <uc2:WebUserControl ID="WebUserControl2" runat="server" />
        <br /><br />
        <asp:Button runat="server" ID="btnOk" Text="ok" OnClick="btnOk_Click" />
    </div>    
</form>

and on code behind this

protected void btnOk_Click(object sender, EventArgs e)
{
    WebUserControl1.TextOnMe = WebUserControl2.TextOnMe;
}

Now the user controls have this on html

<asp:TextBox runat="server" ID="txtText"></asp:TextBox>

and on code behind you get and set the Text like that on code behind

public string TextOnMe
{
    get
    {
        return txtText.Text;
    }
    set
    {
        txtText.Text = value;
    }
}

One Similar Answer ASP.NET MasterPage only control?

Aristos
  • 66,005
  • 16
  • 114
  • 150