Solved

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

Posted on 2009-05-04
4
463 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
ID: 24299958
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
ID: 24303750
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
ID: 24303752
also that change if the user clicks cancel on the confirm it will stay disabled.
0
 

Author Comment

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

UpdatePanel.Update();

same deal
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I prevent the pages in my site from showing up in a google search? 2 72
Web site error 3 35
Graphics 2 26
Problem to refer to value 8 45
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

11 Experts available now in Live!

Get 1:1 Help Now