Treeview problem, get value from selected node in another usercontrol ? asp.net 2.0

J

jesper_lofgren

Hi,

I have two usercontrols one menu control that use ASP.NET 2.0 Treeview
control, then a main usercontrol for showing the selected URL in a
Iframe.

The problem is when i raise the OnSelectedNodeChanged on the menu and
add the URL value into a session or Viewstate key and collect the value
on the second (main usercontrol) the value is always i step behind the
actual click value ?

Anyone have a solution on this ? can i get the value on the Treeview in
some other way then storing in a seperate session/viewstate key ?

Thanks
Jesper
 
D

Daniel TIZON

Hi Jesper,
I think your Main Usercontrol try to get the value too much early, in the
Page_Load.
Try to get the value in the Page_PreRender event handler.

Personally when I have to do a communication between User Controls, I raise
en event from the Provider Control,
I subscribe to the event from the Page which holds the controls, and in the
event handler I send a message to a public method of the Consumer Control.

I Hope this helps,
 
J

jesper_lofgren

Hi, thanks for your answer. Think you have right that the main
usercontrol is trying to get the value before a new has been set.
Will try to get it in the PreRender method.

But if you have time i would really like to see a short exemple of your
solution for it. What to learn a new way to handle this and your
solution sounds intressting.

Thanks
Jesper
 
D

Daniel TIZON

Ok, here is the scenario and the solution:
-I have 2 WebUserControls :
ProviderControls.ascx : it contains a TextBox and a Button.
ConsumerControl.ascx : it contains a Label
I have a WebForm called HostPage.aspx
The communication consist to pass the content of the textbox to the label
only when we click on the button

Code in App_Code folder : MyHandlers.cs
--------------------------------------------
public delegate void ConsumerControlEventHandler(object sender,
ConsumerControlEventArg e);
public class ConsumerControlEventArg : EventArgs
{
public string TheValue = string.Empty;
}

Code in ProviderControl.ascx.cs
-----------------------------------
public partial class UserControlsCommunication_ProviderControl :
System.Web.UI.UserControl
{
public event ConsumerControlEventHandler NewValue;
protected void cmdOK_Click(object sender, EventArgs e)
{
ConsumerControlEventArg arg = new ConsumerControlEventArg();
arg.TheValue = txtValue.Text;
NewValue(this, arg);
}
}

Code in ConsumerControl.ascx.cs
-----------------------------------
public partial class UserControlsCommunication_ConsumerControl :
System.Web.UI.UserControl
{
public string Param1
{
get
{
String s = (String)ViewState["Param1"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Param1"] = value;
}
}
void Page_PreRender(object sender, EventArgs e)
{
lblResult.Text = Param1;
}
}

Code in HostPage.aspx.cs
---------------------------
public partial class UserControlsCommunication_HostPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ProviderControl1.NewValue += new
ConsumerControlEventHandler(ProviderControl1_NewValue);
}

void ProviderControl1_NewValue(object sender, ConsumerControlEventArg e)
{
ConsumerControl1.Param1 = e.TheValue;
}
}


Have fun :)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top