colobusgem
colobusgem

Reputation: 473

What are techniques in ASP for communication from usercontrol to parent page?

I have having some trouble with communication from a usercontrol to the main page. The order in which events are raised means that the action on the user control occurs too late in the post back to have an effect on the main page.

For example, I have a button on a user control which, when pressed, raises a custom event that is being listened for on the main page. When the button is pressed the postback order is:

  • page_load - on the main page

  • page_load - on the usercontrol (the user control is loaded programitically by the main page page_load)

  • The button call back on the user control

  • The event call back method on the main page

By this point, it seems it is too late for anything the event call back method does to have any effect on the rendered page, for example I am trying to use it to change the usercontrol that is being displayed.

What other techniques can be used for this kind of communication?

Relevant code Main page:

    public string LastLoadedControl
    {
        get
        {
            return Session["LastLoaded"] as string;
        }
        set
        {
            Session["LastLoaded"] = value;
        }
    }

    private void LoadUserControl()
    {
        string controlPath = LastLoadedControl;

        ContentPlaceholder.Controls.Clear();

        if (string.IsNullOrEmpty(controlPath))
            controlPath = Utils.Paths.USERCTRL_BASE + "Main.ascx";

        Control uc = Page.LoadControl(controlPath);
        ContentPlaceholder.Controls.Add(uc);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadUserControl();
        if (!IsPostBack)
            Utils.Events.redirectPage += Events_redirectPage;

    }

    private void Events_redirectPage(string path)
    {

        if (path.Equals("Main"))
        {
            //Session.Clear();
            //Session.Abandon();
        }
        else LastLoadedControl = Paths.USERCTRL_BASE + path + ".ascx"
        LoadUserControl();
    }

User control

    protected void profileBtn_Click(object sender, EventArgs e)
    {
        Utils.Events.triggerRedirectPage("Login");
    }

Event

public class Events
{
    public delegate void redirectEvent(string path);
    public static event redirectEvent redirectPage;

    public static void triggerRedirectPage(String path)
    {
        if (Utils.Events.redirectPage != null)
            Utils.Events.redirectPage(path);
    }
}

Upvotes: 0

Views: 343

Answers (1)

Parthasarathy
Parthasarathy

Reputation: 2818

There are two approaches that you can follow.

Approach 1:

public interface IEventProvider
{
  void TriggerEvent();  
}

public class YourPage: Page, IEventProvider
{
   // Other page methods

   public void TriggerEvent()
   {
     // Your Implementation
   }
}

public class YourUserControl : WebUserControl
{
    protected void profileBtn_Click(object sender, EventArgs e)
    {
        IEventProvider eventProvider = this.Page as IEventProvider;
        if(eventProvider != null)
           eventProvider.TriggerEvent();
    }
}

Approach 2:

public interface IEventProvider
{
    // This does not have to be a boolean. You can use a string / enum / anything that suits your implementation
    bool Trigger {get; set;}
}

public class YourPage: Page, IEventProvider
{
   // Other page methods

   protected override void OnLoadComplete(EventArgs e)
   {
     // This will be raised when all the events have fired for all the controls in the page. 
     if(this.Trigger)
        TriggerEvent();
   }

   protected void TriggerEvent()
   {
     // Your code here
   }

   public bool Trigger
   {
     get;
     set;
   }
}

public class YourUserControl : WebUserControl
{
    protected void profileBtn_Click(object sender, EventArgs e)
    {
        IEventProvider eventProvider = this.Page as IEventProvider;
        if(eventProvider != null)
           eventProvider.Trigger = true;
    }
}

Upvotes: 1

Related Questions