locked
How to get a custom control to trigger and UpdatePanel to refresh. RRS feed

  • Question

  • User-1959374973 posted

    I have an update panel that contains a multi-line textbox.  I have a custom control that I'd like to have trigger the update panel to refresh.  Everything is working with the exception of the update panel updating.  Events are getting triggered properly.  But no update to the update panel.  

    Below is my code for my page:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:scriptmanager ID="Scriptmanager1" runat="server" EnablePartialRendering="true" ></asp:scriptmanager>
            <asp:FileUpload ID="FileUpload" runat="server" />
            <br />
            <asp:Button ID="LoadFileButton" runat="server" Text="Load File" OnClick="LoadFileButton_Click" />
            <asp:Label ID="FileNameLabel" runat="server" Visible="false" />
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
                <ContentTemplate>
                    <asp:Panel ID="Panel1" runat="server">
                        <asp:TextBox ID="OutputTextbox" runat="server" Width="640px" Height="320px" TextMode="MultiLine" ReadOnly="True"  />
                    </asp:Panel>
                    <br />
                    <asp:Button ID="ProcessButton" runat="server" Text="Process" Enabled="false" Visible="false" OnClick="ProcessButton_Click" />
                </ContentTemplate>
                <Triggers>
                    <asp:PostBackTrigger ControlID="LoadFileButton" />
                </Triggers>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>

    Here is my code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                this.InitControls();
            }
    
            protected static bool ProcessStarted = false;
    
            protected static string Message;
    
            protected TriggerControl TriggerControl;
    
            protected void InitControls()
            {
                this.TriggerControl = new TriggerControl();
                this.TriggerControl.ID = "TriggerControl";
                this.TriggerControl.EventTriggered += this.TriggerControl_EventTriggered;
    
                this.Scriptmanager1.RegisterAsyncPostBackControl(this.TriggerControl);
    
                this.Page.Controls.Add(this.TriggerControl);
    
                UpdatePanel up = this.Page.FindControl("UpdatePanel1") as UpdatePanel;
    
                if (up != null)
                {
                    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
                    trigger.ControlID = this.TriggerControl.ID;
                    trigger.EventName = "EventTriggered";
    
                    up.Triggers.Add(trigger);
                }
            }
    
            protected void WorkerFunction()
            {
                Message = "";
    
                // Create worker class.
                ImportController import = new ImportController(10);
                // Add event handlers.
                import.ProcessFinished += import_ProcessFinished;
                import.ProcessStarted += import_ProcessStarted;
                import.ProgressChanged += import_ProgressChanged;
                // Process the work.
                import.Process();
                // Remove event handlers.
                import.ProcessFinished -= import_ProcessFinished;
                import.ProcessStarted -= import_ProcessStarted;
                import.ProgressChanged -= import_ProgressChanged;
            }
    
            protected void LoadFileButton_Click(object sender, EventArgs e)
            {
                HttpContext.Current.Session["FileNameToProcess"] = this.FileUpload.FileName;
                HttpContext.Current.Session["FileBytesToProcess"] = this.FileUpload.FileBytes;
    
                this.FileUpload.Enabled = false;
                this.FileUpload.Visible = false;
    
                this.LoadFileButton.Enabled = false;
                this.LoadFileButton.Visible = false;
    
                this.FileNameLabel.Text = String.Format("File to be processed: {0}", this.FileUpload.FileName);
                
                this.FileNameLabel.Enabled = true;
                this.FileNameLabel.Visible = true;
    
                this.ProcessButton.Enabled = true;
                this.ProcessButton.Visible = true;
            }
    
            protected void ProcessButton_Click(object sender, EventArgs e)
            {
                if (!ProcessStarted)
                {
                    ProcessStarted = true;
    
                    string name = HttpContext.Current.Session["FileNameToProcess"].ToString();
    
                    Thread worker = new Thread(new ThreadStart(this.WorkerFunction));
                    worker.Start();
    
                    return;
                }
            }
    
            protected void import_ProcessFinished(object sender, EventArgs e)
            {
                ProcessStarted = false;
            }
    
            protected void import_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                Message += e.ProgressMessage;
                this.TriggerControl.InvokeEventTriggered();
            }
    
            protected void import_ProcessStarted(object sender, EventArgs e)
            {
                this.ProcessButton.Enabled = false;
            }
    
            protected void TriggerControl_EventTriggered(object sender, EventArgs e)
            {
                if (Message != "")
                {
                    this.OutputTextbox.Text = this.OutputTextbox.Text + Message;
                    Message = "";
                }
            }
    
        }
    
        public class TriggerControl : Panel, IPostBackDataHandler
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            public void InvokeEventTriggered()
            {
                this.OnEventTriggered(this, new EventArgs());
            }
    
            // Delegate
            public delegate void EventTriggeredHandler(object sender, EventArgs e);
            // Event
            public event EventTriggeredHandler EventTriggered;
    
            /// <summary>
            /// Fires the EventTriggered event.
            /// </summary>
            /// <param name="sender">Object representing the sender of the event.</param>
            /// <param name="e">ProgressChanged event arguments.</param>
            protected void OnEventTriggered(object sender, EventArgs e)
            {
                if (EventTriggered != null)
                {
                    EventTriggered(this, e);
                }
            }
    
            public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                return true;
            }
    
            public void RaisePostDataChangedEvent()
            {
                
            }
        }

    There is a file that gets uploaded using the FileUpload control first.  It posts back and the page updates a label with the name of the file that is going to be processed.  Pressing the Process button then creates a thread and calls a function to do some work on the file that was uploaded.  That process will then fire events every so often reporting back the status of the job.  That event handler will then invoke the TriggerControl's EventTriggered event, which should then cause the UpdatePanel to refresh.  The UpdatePanel is not refreshing and I don't know why.  All the events are triggering properly.

    If anyone has any ideas on how to make this work please let me know.

    Thursday, April 24, 2014 4:17 PM

All replies

  • User1208776063 posted

    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
                    trigger.ControlID = this.TriggerControl.ID;
                    trigger.EventName = "EventTriggered";
    
                    up.Triggers.Add(trigger);
    

    I don't think you can add an asyncpostbacktrigger programmatically. You just need to use RegisterAsyncPostBackControl method of the scriptmanager.

    Scriptmanager1.RegisterAsyncPostBackControl(this.TriggerControl);

    If you want to know more about this method, read this MSDN documentation. Otherwise, you could just remove UpdateMode="conditional" ChildrenAsTriggers="false"(This could be an issue as well) from the update panel markup. This will make sure that panel is refreshed for all aysncpostbacks.

    Thursday, April 24, 2014 8:15 PM
  • User-1959374973 posted

    I've tried it with programmatically adding the trigger (with the code creating the trigger, setting it's ControlID and EventName), and without that code, just calling function with the script manager.  It doesn't work in both cases.

    I feel like I'm missing something obvious.

    Friday, April 25, 2014 9:09 AM
  • User-933407369 posted

    As dotnetzoom said, you don't  use the codes:

    Scriptmanager1.RegisterAsyncPostBackControl(this.TriggerControl);

    then

    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
                    trigger.ControlID = this.TriggerControl.ID;
                    trigger.EventName = "EventTriggered";
    
                    up.Triggers.Add(trigger);

    You'd better debug your codes step by step. if possible, could you post the complete  markup or provide a simple demo,which can easily reproduce the problem?

    In order to help us to understand your issue better.

    Thanks.

    Monday, April 28, 2014 2:24 AM
  • User-1959374973 posted

    You'd better debug your codes step by step. if possible, could you post the complete  markup or provide a simple demo,which can easily reproduce the problem?

    In order to help us to understand your issue better.

    Thanks.

    The code posted in the initial post is basically everything from that page, markup and code behind.  As far as debugging goes, all of the event handlers trigger properly.  The only thing not happening is the refreshing of the multi-line TextBox in the UpdatePanel.

    Monday, April 28, 2014 8:43 AM
  • User-227760790 posted

    Sorry, i try the codes above but it is incomplete and doesn't reproduce the issue.  please provide more codes which is related to the issue.

    Monday, April 28, 2014 9:27 PM