469,578 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

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

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

Dec 17 '05 #1
3 2238
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,

--
Daniel TIZON
MCP - MCSD.NET - MCT

<je************@yahoo.se> a écrit dans le message de news:
11*********************@z14g2000cwz.googlegroups.c om...
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

Dec 17 '05 #2
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

Dec 17 '05 #3
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_NewVa lue);
}

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

--
Daniel TIZON
MCP - MCSD.NET - MCT

<je************@yahoo.se> a écrit dans le message de news:
11*********************@g47g2000cwa.googlegroups.c om...
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

Dec 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Karim El Jed | last post: by
7 posts views Thread by vsiat | last post: by
5 posts views Thread by Juanjo | last post: by
2 posts views Thread by Claus | last post: by
8 posts views Thread by Matt MacDonald | last post: by
2 posts views Thread by rfm | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.