Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

Delay Loading in ASP.NET AJAX

Thursday, 29 January 2009 09:01 by ugur

You have content that will be uploaded synchronously in your page and content that will be uploaded asynchronously in UpdatePanel. However, the upload process of UpdatePanel content may take more time… If I give more concrete example, the upload process of normal content may take 2 seconds and the upload process of asynchronous content may take 7-8 seconds. If we wait for the whole page uploaded under normal circumstances and then send to the client, we will keep the client waiting at least 7-8 seconds. Well, what if we send the synchronous content to the browser of the client during the first upload of the page while the UpdatePanel continues to upload its own content? I guess you are curious about how to do such a process. In this post, we will search for how to delay loading process in ASP.NET AJAX applications.

First, the needed ingredients; 1 ScriptManager, 1 Label (for synchronous content), 1 UpdatePanel, 1 Button inside the panel and 1 more Label for asynchronous content (if we want our page more attractive, 1 UpdateProgress). The first label will hide the normal content, in other words this control will go to the client after the first upload. The other label is for asynchronously updated content, in other words the upload process in here will be performed just after the page reached to the client. The HTML codes and the code snippet that we will use to update the UpdatePanel asynchronously are given below.

Default.aspx

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

<asp:Label ID="lblStatik" runat="server"></asp:Label>

<br /><br />      

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

        <asp:LinkButton ID="lbGuncelle" runat="server" Text="Güncelle" onclick="lbGuncelle_Click"></asp:LinkButton><br />

        <asp:Label ID="lblAsenkronIcerik" runat="server"></asp:Label><br />

        <asp:UpdateProgress ID="UpdateProgress1" runat="server">

            <ProgressTemplate>

                Loading. Please wait...

            </ProgressTemplate>

        </asp:UpdateProgress>

    </ContentTemplate>

</asp:UpdatePanel>


Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

    lblStatik.Text = "Static content will be here";

}

protected void lbGuncelle_Click(object sender, EventArgs e)

{

    System.Threading.Thread.Sleep(3000); //For delaying 

    lblAsenkronIcerik.Text = DateTime.Now.ToString();

}

As can be seen the LinkButton control named lbGuncelle updates UpdatePanel content. The page as it is will not create any output in UpdatePanel after the first upload process. But, if we make the LinkButton automatically postbacks when the page is uploaded at the first time, at the same time UpdatePanel content will reach to the client. If we can perform postback process at the same time with the upload of the page at the client side, the upload process will be delayed. There are two questions to be answered in here; first, "how can I catch the exact moment of upload of the page in the client side?", second, "how can I trigger the partial postback process without clicking LinkButton?" Because both of these processes eventuate at the client side, JavaScript must be used. At the moment that the page is loading, the pageLoad function of JavaScript is stepped in. On the other hand, one of the other functions of JavaScript named __doPostBack is used for to apply postback process to a control programmatically. The JavaScript pageLoad function given below will provide the delay in loading the UpdatePanel content during the loading of the page at the first time.

Default.aspx

<script language="javascript" type="text/javascript">

    var firstLoad = true;

    function pageLoad()

    {

        if(firstLoad)

        {

            __doPostBack('lbGuncelle', ''); //Programatically postback for LinkButton control

            firstLoad = false;

        }

    }

</script>

pageLoad function executes while the page is loading to the client. Because the firstLoad variable is true at this moment, LinkButton control makes the postback process and UpdatePanel content is started to be updated. Because we delay loading with Sleep method of Thread class, we can see the delay in updating UpdatePanel content clearly when the page is uploaded to the client.

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (5) | Comment RSSRSS comment feed

ASP.NET MVC 1.0 RC Has Been Released

Wednesday, 28 January 2009 11:12 by ugur

ASP.NET MVC 1.0 RC has been released today, after Internet Explorer 8.0 RC1 that was released yesterday. This important announcement excited me like many ASP.NET developers. You can follow the links above to get more detailed information about RC version. 

Scott Guthrie - ASP.NET MVC 1.0 Release Candidate Now Available
David Hayden - Download ASP.NET MVC 1.0 Release Candidate

Opening New Window with Response.Redirect() Method

Thursday, 15 January 2009 23:47 by ugur

In some occasions, we can direct the user to a specific page by using Response.Redirect method in the code-behind side. Well, how can we make the directed page to be displayed in a new browser window? In fact, the answer of this question is not related to Response.Redirect method. This instance can be solved completely by an operation done on the trigger control of this method. For example, if button control carry out the direct process, then the code snippet can be seen below will be enough. By the way, I think it isn't necessary to mention that Button1 control is involved in the form element named form1:)

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click Me" OnClientClick="form1.target='_blank';" />

It will be adequate to add standard Redirect method to the Click event method of Button.

protected void Button1_Click(object sender, EventArgs e)

{

    Response.Redirect("Default.aspx");

}

But, how is the process done? form1.target='_blank' expression added in Button1 control and functions in client side transfers the operation to be done in a new window. Thus, the page makes postback on the new page and is directed to Default.aspx page.

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

"You tried to assign the Null value to a variable..." Error and Solution

Tuesday, 6 January 2009 12:44 by ugur
When you use a table formed in MS Access with AccessDataSource control in Insert processes, you might come across an error like "You tried to assign the Null value to a variable that is not a Variant data type". Even if there is not a null value in your query, this error will occur because the identity column of your table has been added to the Insert statement in Visual Studio as the data input column. The solution is actually very simple, you should remove the name and parameter (the ? character in Value region) of identity column (is usually primary key column) from InsertCommand property of AccessDataSource control in HTML codes. At last, you can successfully execute Insert comment after you have removed the parameter added for identity column in InsertParameters collection.
Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
 
Add to Technorati Favorites