Solved

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

Posted on 2009-05-04
4
460 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

13 Experts available now in Live!

Get 1:1 Help Now