Domanda

I have a UserControl that functions as a template for a FormView, But depending on whether it is in edit or insert mode, one of the TextBox controls needs to be disabled. I added a function to the UserControl

public bool IsInsert
{
    get { return txtUser.Enabled; }
    set { txtUser.Enabled = value; }
}

But I cannot get a reference of the UserControl in the parent's Page_Load event. I defined the control in the aspx code (not code-behind). I've tries using FindControl but I get an error Object reference not set to an instance of an object. Is this because the UserControl loads after the page? Is there another method of disabling the TextBox conditionally?

È stato utile?

Soluzione

Its not good practice - User Control should decide this kind of stuff alone...

But if it need to be done this way:

public void Page_Load(object sender, EventArgs e)
{
    InitYouUserControl();
}

Update:

You have to wait till the load event of your User Control is fired.

And to access some parts of User Control you should define a property in it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top