Accessing a property in a webcontrol from parent page

  • Thread starter Thread starter spmm#
  • Start date Start date
S

spmm#

Hi!

Could someone please help me with the following; I have a WebControl that
basically looks like this:

public class LeftMenu : System.Web.UI.WebControls.WebControl
{
private string m_CurrentNodeId = string.Empty;

public string CurrentNodeId
{
get
{
return m_CurrentNodeId;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
...
}

private void custExpand_Click(object sender,EventArgs e)
{
m_CurrentNodeId = e.SomeStringValue;
}
}

Which I'm loading into my parent page like this:

public class Default
{
protected LeftMenu mmLeftMenu;
protected System.Web.UI.WebControls.Panel pnlLeftMenu;
public string CurrentNodeId = String.Empty;

private void Page_Load(object sender, System.EventArgs e)
{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
CurrentNodeId = mmLeftMenu.CurrentNodeId;
}
}

Problem is CurrentNodeId in Default never gets the value of
mmLeftMenu.CurrentNodeId, because custExpand_Click gets fired after the
Page_Load in Default. How should I set things up, so that I can get the
value of mmLeftMenu.CurrentNodeId in Default???

Thanks.
 
What you have here is an issue of sequence. This problem is fairly common,
esp. among newer developers of ASP.Net, as the .Net SDK seems to have a lot
of samples using the Page_Load event. In point of fact, however, the
system.Web.UI.Page class is derived from System.Web.UI.Control, and every
Control has about a dozen event handlers that you can wire up, all of which
occur in a specific sequence. To find out more about the sequence of events,
see:

http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
You might need to make your webcontrol raise an event when custExpand_Click
is hit, which the page can hook into...something like

public class LeftMenu: WebControl{
public event CommandEventHandler NodeClick;
...
private void custExpand_Click(object sender,EventArgs e) {
m_CurrentNodeId = e.SomeStringValue;
if (this.NodeClick != null){
CommandEventArgs ev = new CommandEventArgs("clicked",
m_CurrentNodeId);
this.NodeClick(this, ev);
}
}
}

the in your page, you can:

page_load{
mmLeftMenu =
(LeftMenu)LoadControl(pnlLeftMenu,@"~\Controls\LeftMenu.ascx");
mmLeftMenu.NodeClick += new CommandEventHandler(mmLeftMenu_NodeClick);
}

private void mmLeftMenu_NodeClick(object sender, CommandEventArgs e) {
Response.Write("node value: " + e.CommandArgument);
}


Karl
 
Back
Top