Home All Groups Group Topic Archive Search About

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

Author
17 Dec 2005 9:50 AM
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

Author
17 Dec 2005 11:27 AM
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,

--
Daniel TIZON
MCP - MCSD.NET - MCT

<jesper_lofg***@yahoo.se> a écrit dans le message de news:
1134813028.247443.78***@z14g2000cwz.googlegroups.com...
Show quote
> 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
>
Author
17 Dec 2005 3:26 PM
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
Author
17 Dec 2005 9:51 PM
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 :)

--
Daniel TIZON
MCP - MCSD.NET - MCT

<jesper_lofg***@yahoo.se> a écrit dans le message de news:
1134833196.913129.23***@g47g2000cwa.googlegroups.com...
Show quote
> 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
>

AddThis Social Bookmark Button