views:

16

answers:

2

I have a web user control (ascx) that exposes an "ID" property. What I want to do is set this property when the SelectedIndexChanged event fires in a gridview in the containing page. However, I can't seem to do it.... Here's my code:

 protected void grdPhysicians_SelectedIndexChanged(object sender, EventArgs e)
{        
    physicians_certif1.mdID = grdPhysicians.SelectedDataKey.ToString();
    mvData.SetActiveView(viewEdit);
    panAdditional.Visible = true;
}

Physicians_certif1 is the user control. It seems the user control is loading before the SelectedIndexChanged event has a chance to set it's property.

Any ideas folks?

A: 

ASP.Net page lifecycles can be hard to understand especially with ascx user controls which also have their own lifecycle. If you are setting the mdID property in Page_Load of either the page or the ASCX control or have hardcoded a default value into it in the XHTML, it is probably being reset after SelectedIndexChanged fires.

Set a breakpoint in grdPhysicians_SelectedIndexChanged, set a watch on physicians_certif1.mdID and step through the code using the debugger.

Daniel Coffman
A: 

Yes, that is exactly what is happening. You should look at (and be familiar with) the following resource:

ASP.Net Page Life Cycle

The page will load, then the control will load, then your events will begin to fire. If you have configuration needs based on event triggers, it is best either to place those configurations in the Page_LoadComplete or Page_PreRender events of the user control in question or apply "Rebinding" instructions in the Set method of your property:

public MyValue MyProperty()
{
  get
  {
    return _myProperty;
  }
  set
  {
    RebindMyControls();
    _myProperty = value;
  }
}
Joel Etherton