Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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