1

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?

Jenius
  • 174
  • 15

1 Answers1

1

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.

Jenius
  • 174
  • 15
MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • I would dislike using it if it isn't good practice... Is there a better solution than using a `User Control` or coding each `FormView` template individually? – Jenius Jul 09 '13 at 14:06
  • @Pyitoechito - updated my answer. If it helpful - upvote it please ) If it is the solution for your question - also accept it – MikroDel Jul 09 '13 at 17:56
  • @Pyitoechito - do you know how to define property? – MikroDel Jul 09 '13 at 18:59
  • I decided to not use a `UserControl` or `FormView`for my current task. Thanks though. – Jenius Jul 09 '13 at 19:10