Solved

ASP.Net/C# - Update Panel - Need a smart jedi

Posted on 2009-05-04
4
456 Views
Last Modified: 2012-05-06
Hello all,

I have a update panel and a timer control.  The OnclientClick event of the button I call a confirm box.  My code then gets entered into on the Onclick event.  I need to disable the button on the click of it and then return when it is completed its processing.  The issue I am having is that the button is in a repeater control so I only handle it in the onclick event I can get the sender.  So what I have is this:

ASPX page: within an update panel and repeater control:

<asp:Button ID="btnPublish" width="85px" runat="server" CommandArgument='<%#Eval("JOB_ID")%>'  CommandName='<%# GetServerName(Eval("ENGINE_IP"))%>' OnClientClick="return confirm('Are you sure you want to Publish PDFs?');" OnClick="btnPublish_Click" Text="Publish" />

In my code behind on the button click:
 protected void btnPublish_Click(object sender, EventArgs e)
        {
            try
            {
                //stop timer until processing completed
                Timer1.Enabled = false;

                Button btnPublish = ((Button)sender);

                btnPublish.Enabled = false;

                if (btnPublish == null)
                    return;

                string results = PublishPDF(int.Parse(btnPublish.CommandArgument), btnPublish.CommandName);

                RepeaterItem ri = btnPublish.NamingContainer as RepeaterItem;
                Label lbl = ri.FindControl("lblPublish") as Label;

                if (results != string.Empty)
                {
                    lbl.Text = results;
                }
                else
                {
                    lbl.Text = "Publish PDFs completed";
                }

                //start timer
                btnPublish.Enabled = true;
                Timer1.Enabled = true;
            }
            catch
            {
                //start timer
                Timer1.Enabled = true;
            }
        }

How can I handle this?  I tried putting this.disabled; prior to the return confirm on the onclientclick, but when I do this the code is not working right and label not coming up.

Thanks all
0
Comment
Question by:sbornstein2
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
Comment Utility
First problem: you're checking if the cast from System.Object (sender) to System.Windows.Forms.Button (btnPublish) was successful, AFTER attempting to invoke a method on it. Therefore, if the cast was unsuccessful, your if statement that checks if it's null will never iterate, because you attempt to access the "Enabled" property directly beforehand, which will immediately throw a System.NullReferenceException. Solution? Move your first if statement up above the part where you disable the button.

Minor design suggestion: instead of your if statements in the middle, to clean up your code, this would be a prime time to take advantage of the ternary operator, like so:

lbl.Text = (results != System.String.Empty) ? results : "Publish PDFs completed";

I think I'm confused as to what exactly it is you're trying to accomplish here--care to elaborate?
0
 

Author Comment

by:sbornstein2
Comment Utility
okay moved the if statement up for cleanup but still back to my issue where I need to disable the btnPublish on the click of the button and when the btnPublish_Click completes enable the button again.  As you can see from the code above the button is in a repeater and then this repeater is in an update panel content container and there is also a timer on the page that gets hit ever few seconds.   So I can handle keeping the state of the enabled based on storing a generic list in session and I store a key and button enabled boolean value where on the timer event I can then check again to make sure the button stays enabled or disabled on the postback.  The issue is getting the button to disable on the actual first client click of the button which I tried by doing this:

Instead of this:
OnClientClick="return confirm('Are you sure you want to Publish PDFs?');"

Added this:
OnClientClick="this.disabled=true;return confirm('Are you sure you want to Publish PDFs?');"

This worked and disabled the button but my code seemed to not complete and see the:

 if (results != string.Empty)
                {
                    lbl.Text = results;
                }
                else
                {
                    lbl.Text = "Publish PDFs completed";
                }

even get hit.
0
 

Author Comment

by:sbornstein2
Comment Utility
also that change if the user clicks cancel on the confirm it will stay disabled.
0
 

Author Comment

by:sbornstein2
Comment Utility
also tried after the btnPublish.Enabled = false I called my update panel.update such as:

UpdatePanel.Update();

same deal
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now