Question

jpollar on Wed, 17 Dec 2014 16:16:16


I'm testing asynchronous processing with SharePoint 2010 and UpdatePanels.

I want the user to get a message on the page that their query is being executed and disable the button they click before the processing even starts.

Unfortunately, my code isn't working that way. It runs everything together and finally shows the message on the screen and disables the button when the query has finished instead of before the processing starts as desired.

I'm adding my code. Any suggestions?

Aspx Page

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestAsynch.aspx.cs" Inherits="TestAsynch.Layouts.TestAsynch.TestAsynch" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<asp:UpdatePanel runat="server" id="MyUpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblMsg" runat="server"></asp:Label>        
        <asp:Button ID="btnSubmit" runat="server" Text="Run" OnClick="btnSubmit_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server" id="UpdatePanel2" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblOutput" runat="server"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Test Asynch
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Test Asynch
</asp:Content>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI;
using Microsoft.SharePoint;

namespace TestAsynch.Layouts.TestAsynch
{
    public partial class TestAsynch : LayoutsPageBase
    {
        private String sql = @"WAITFOR DELAY '00:00:10';
                        SELECT count(*) 
                        FROM Users;";

        private String connectionString = @"Data Source=sql;Initial Catalog=Temp;Integrated Security=True; Asynchronous Processing=true";

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)            
                lblOutput.Text = "";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            lblMsg.Text = "Executing Query. The time right now is: " + DateTime.Now.ToLongTimeString();
            btnSubmit.Enabled = false;
            MyUpdatePanel.Update();
                
            PageAsyncTask task = new PageAsyncTask(BeginDatabaseCall,

                                    EndDatabaseCall,

                                    TimeoutDatabaseCall,

                                    null,

                                    true);

            Page.AsyncTimeout = TimeSpan.FromSeconds(30);

            Page.RegisterAsyncTask(task);

        }

        
        private IAsyncResult BeginDatabaseCall(Object sender, EventArgs e, AsyncCallback cb, Object state)
        {

            IAsyncResult result = null;
            
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                
                SqlConnection connection =

                             new SqlConnection(connectionString);

                SqlCommand command =

                             new SqlCommand(sql, connection);

                connection.Open();

                result = command.BeginExecuteReader(cb, command);

                
            });

            return result;

        }

        private void EndDatabaseCall(IAsyncResult result)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate
            {

                SqlCommand command = (SqlCommand)result.AsyncState;

                using (SqlDataReader reader = command.EndExecuteReader(result))
                {

                    reader.Read();

                    lblOutput.Text =

                        "<br /><b>Count: "

                        + reader[0].ToString() + "</b>";

                }

                command.Connection.Close();

                command.Dispose();

            });

            
            ChildControlsCreated = true;

        }

        private void TimeoutDatabaseCall(IAsyncResult result)
        {
            lblOutput = new Label();
            lblOutput.Text = "Timed out.<br />";

        }

    }
}


Sponsored



Replies

Jerry Zy on Thu, 18 Dec 2014 09:59:25


Hi jpollar,

According to your description, my undrstanding is that you want to execute the event asynchronously.

In your code snippet, there is no script manager control, the updatepanel works rely on the scirptmanager control.

You can add the script manager control to page like below:

<asp:ScriptManager ID="ScriptManager1" 
                   runat="server" />

Here is a detailed code demo for your reference:

UpdatePanel Control Overview

Best Regards

jpollar on Thu, 18 Dec 2014 22:29:56


It's in SharePoint 2010. A ScriptManager already exist in the master page.

If I didn't have one at all, I'd be getting errors.

Adding another causes errors. "Only one instance of a ScriptManager can be added to the page."

Is that not obvious???

Jerry Zy on Fri, 19 Dec 2014 01:11:40


Hi jpollar,

I suggest you remove the script manager from the master page and add it to the current page to test again.

Also, please debug your code step by step to find if there is something wrong in it.

Best Regards


Zhengyu Guo
TechNet Community Support


Vivek.G on Fri, 19 Dec 2014 05:56:23


Hi jpollar,

I've done this within Update panel, disabling button on button click. Please look at the code below.

Add this to your btnSubmit,

OnClientClick="hideBtn();"

Then add following javascript.

function hideBtn() {

document.getElementById("<%= btnSubmit.ClientID %>").disabled = true;

}.

Javascript gets executed before server events takes place.

Thanks,
Vivek
Please vote or mark your question answered, if my reply helps you

jpollar on Mon, 22 Dec 2014 23:14:13


Vivek,

Thank you. Good call.

I can update other items on the page also if needed.

Vivek.G on Tue, 23 Dec 2014 05:03:11


Hi jpollar,

Good to hear it.

Please mark answered if it helped your issue.

Thanks,

Vivek