Accessing a property in a webcontrol from parent page

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.
 
K

Kevin Spencer

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
 
K

Karl Seguin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top