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

Discussion in 'ASP .Net' started by jesper_lofgren@yahoo.se, Dec 17, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Daniel TIZON Guest

    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

    <> a écrit dans le message de news:
    ...
    > 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
    >
    Daniel TIZON, Dec 17, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. Daniel TIZON Guest

    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

    <> a écrit dans le message de news:
    ...
    > 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
    >
    Daniel TIZON, Dec 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wolf
    Replies:
    0
    Views:
    540
  2. Replies:
    2
    Views:
    12,439
    ro0oraa
    Sep 2, 2012
  3. =?Utf-8?B?VHltYm93?=
    Replies:
    2
    Views:
    24,413
    =?Utf-8?B?VHltYm93?=
    Jan 10, 2006
  4. =?Utf-8?B?V2Vz?=
    Replies:
    2
    Views:
    18,795
    sankar
    Jul 3, 2006
  5. Naim Rizk
    Replies:
    2
    Views:
    715
    rune solheim
    Sep 23, 2003
Loading...

Share This Page