Solved

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

Posted on 2009-05-04
4
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

707 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