Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for the ‘ASP.NET Ajax’ Category

Show "Loading" image until grid is populated?

Posted by Ramani Sandeep on September 9, 2009

Hi Readers,

Today I have faced one problem in which i have to display Loading image with progress bar when i update content of Datalist / Gridview. So i have done some googling on this topic so that i can resolve this problem fast.

I always believe that if some of our friends have already solved such issues so why not i use their solutions.

During my googling i found very useful articles written by Matt Berseth, so here i am sharing it with you people so that you can also get benefit from that.

These articles has every thing that developer need to display different style of loading UI.

UI special effect are really add special attraction in web application & these kind of small small effect make it more user friendly & effective.

Hope this helps to you also !!!

Jai Ganesh

Posted in ASP.NET Ajax | Tagged: , , | 1 Comment »

Using jQuery to directly call ASP.NET AJAX page methods

Posted by Ramani Sandeep on September 3, 2009

Here I am looking to explain how to call page methods using jQuery.

Step 1: Define your Page Methods in code behind:

[WebMethod]
    public static int MyMethod1()
    {
        return 13;
    }
    [WebMethod()]
    public static string MyMethod2(string a, int b)
    {
        return a + " –> " + b.ToString();
    } 

Step 2: Include the jQuery Library on Default.aspx page:

<script src="Js/jquery.min.js" type="text/javascript"></script> 

If you do not have jQuery file in Js folder then use following:

<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"> 

Step 3: Now Write the code to Call Page method:

<script type="text/javascript">
        function PageMethod(fn, paramArray, successFn, errorFn) 
        { 
            var pagePath = window.location.pathname; 
            //Create list of parameters in the form : {"paramName1":"paramValue1","paramName2":"paramValue2"} 
            var paramList = ”; 
            if (paramArray.length > 0) 
            { 
                for (var i=0; i<paramArray.length; i+=2) 
                { 
                    if (paramList.length > 0)
                        paramList += ‘,’; 
                    paramList += ‘"’ + paramArray[i] + ‘":"’ + paramArray[i+1] + ‘"’; 
                } 
            } 
            paramList = ‘{‘ + paramList + ‘}’; 
            //Call the page method 
            $.ajax({ 
                type: "POST", 
                url: pagePath + "/" + fn, 
                contentType: "application/json; charset=utf-8", 
                data: paramList, 
                dataType: "json", 
                success: successFn, 
                error: errorFn 
            }); 
        } 
        function AjaxSucceeded (result)
        {
            alert(result.d);
            $("#Result").text("Result : " + result.d);
        }
        function AjaxFailed (result)
        {
            alert("Error on Page");
        }
        function CallPageMethod1()
        {          
            PageMethod("MyMethod1", [], AjaxSucceeded, AjaxFailed);                       
            return false;
        }
         function CallPageMethod2()
        {     
            PageMethod("MyMethod2",["a", "value", "b", 2], AjaxSucceeded, AjaxFailed);           
            return false;
        }
    </script> 

Step 4: To Test your code use following html on default.aspx:

<form id="form1" runat="server">
        <h1>
            Using jQuery To Call ASP.NET Page Methods and Web Services</h1>
        <div> 
<asp:Button ID="Button1" runat="server" Text="With No Parameter" OnClientClick="return CallPageMethod1();" />
   <asp:Button ID="Button2" runat="server" Text="With Parameter" OnClientClick="return CallPageMethod2();" />
        </div>
        <div id="Result">
        </div>
    </form> 

jQuery makes an AJAX call to the MyMethod1 & MyMethod2 page method and replaces the div’s text with its result.

Very Simple!!!

Hope You will also get benefit from this.

Jai Ganesh

Related Post:

http://ramanisandeep.wordpress.com/2009/09/22/calling-web-service-methods-using-asp-net-ajax/

Posted in ASP.NET Ajax, JQuery, Web Services | Tagged: , , , , , , | 8 Comments »

jQuery Tutorials ( Learn – Practice – Expert )

Posted by Ramani Sandeep on August 31, 2009

Article 1: Using jQuery With ASP.NET

This article has everything that is needed by programmer who want to start learning jQuery.

It includes the following topics:

  • Using jQuery in ASP.NET Page
  • jQuery Selectors
  • jQuery Chainability
  • jQuery Object Accessors
  • jQuery Events
  • Ajax using jQuery and ASP.NET
  • jQuery Ajax with JSON data
  • Effects with jQuery

Read Full Article: click here…

Article 2: Using jQuery to consume ASP.NET web services

First, It’ll cover the two requirements necessary when calling an ASMX web service that’s being JSON serialized by the ASP.NET AJAX extensions.
Then, It’ll show you how to do this with jQuery.
Finally, I’ll update the deferred content loading example accordingly.

Read Full Article: click here



Article 3: ON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks

Recently some reports have been issued by security researchers describing ways hackers can use the JSON wire format used by most popular AJAX frameworks to try and exploit cross domain scripts within browsers.  Specifically, these attacks use HTTP GET requests invoked via an HTML <script src=""> include element to circumvent the "same origin policy" enforced by browsers (which limits JavaScript objects like XmlHttpRequest to only calling URLs on the same domain that the page was loaded from), and then look for ways to exploit the JSON payload content.

Read Full article: click here

Posted in ASP.NET Ajax, JQuery | Tagged: , , , , , , | Leave a Comment »

JSON.Net

Posted by Ramani Sandeep on August 31, 2009

The Json.NET library makes working with JavaScript and JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer.

Features

  • LINQ to JSON
  • The JsonSerializer for quickly converting your .NET objects to JSON and back again
  • Json.NET can optionally produce well formatted, indented JSON for debugging or display
  • Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
  • Ability to convert JSON to and from XML
  • Supports multiple platforms: .NET, Silverlight and the Compact Framework

The JSON serializer is a good choice when the JSON you are reading or writing maps closely to a .NET class. The serializer automatically reads and writes JSON for the class.

For situations where you are only interested in getting values from JSON, don’t have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects then LINQ to JSON is what you should use. LINQ to JSON allows you to easily read, create and modify JSON in .NET.

Json.NET CodePlex Project

Json.NET Download

Posted in ASP.NET Ajax, JavaScript, JQuery, Linq | Tagged: , , | Leave a Comment »

Using jQuery Grid With ASP.NET MVC

Posted by Ramani Sandeep on August 27, 2009

A common scenario when building web user interfaces is providing a pageable and sortable grid of data. Even better if it uses AJAX to make it more responsive and snazzy. Since ASP.NET MVC includes jQuery, I figured it’d be fun to use a jQuery plugin for this demo, so I chose jQuery Grid.

After creating a standard ASP.NET MVC project, the first step was to download the plugin and to unzip the contents to my scripts directory per the Installation instructions.

 

Click here to read more…..

Posted in ASP.NET Ajax, ASP.NET MVC, JQuery | Tagged: , , , | 2 Comments »

AsyncPostBackTrigger vs PostBackTrigger

Posted by Ramani Sandeep on April 7, 2009

In the <triggers> template in an update panel, there are the options of an AsyncPostBackTrigger or a PostBackTrigger.

By default, controls outside of an update panel will trigger a normal synchronous post back.  The AsyncPostBackTrigger “wires” up these controls to trigger an asynchronous post back.  Conversely, controls declared inside an update panel will trigger an asynchronous call by default.  The PostBackTrigger short circuits this, and forces the control to do a synchronous post back.

Utilizing a simple “time” example:

 <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            Page Generated @
            <asp:Label runat="server" ID="uiPageTime" />
            <p />
            <asp:UpdatePanel runat="server" ID="update" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Label runat="server" ID="uiTime" />
                    <asp:Button runat="server" ID="uiInternalButton" Text="Click" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="uiAsynch" EventName="click" />
                    <asp:PostBackTrigger ControlID="uiInternalButton" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:Button runat="server" ID="uiPostback" Text="Click" />
            <asp:Button runat="server" ID="uiAsynch" Text="Asynch" />
        </div>
    </form>

And the code behind file.

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            uiAsynch.Click += uiAsynch_Click;
            uiPostback.Click += uiPostback_Click;
            uiInternalButton.Click += uiInternalButton_Click;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            uiPageTime.Text = DateTime.Now.ToLongTimeString();

            if (!IsPostBack)
            {
                uiTime.Text = DateTime.Now.ToLongTimeString();
            }
        }

        private void uiInternalButton_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Internal @ " + DateTime.Now.ToLongTimeString();
        }

        private void uiPostback_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Postback click @ " + DateTime.Now.ToLongTimeString();
            update.Update();
        }

        private void uiAsynch_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Asych click @ " + DateTime.Now.ToLongTimeString();
            update.Update();
        }
    }

Posted in ASP.NET Ajax | Tagged: | 16 Comments »

JavaScript Access to Page Controls after Ajax Update (via Update Panel)

Posted by Ramani Sandeep on April 7, 2009

Several times, we are in a situation where we need to do some work the moment the update panel finishes its update (ASP.NET 2.0 AJAX). There are situations like showing an error if any during the update, showing a success message after a proper update, setting up scroll bars, changing some UI component, updating some other part of UI, etc. The list can get longer as per the developer’s requirement. I had tried to show how we can get control to the page once the update is finished. We can tweak things via JavaScript the way we need to out there in the handler.

Click Here for more Details

Posted in ASP.NET Ajax, JavaScript | Tagged: , | Leave a Comment »

How to refresh an UpdatePanel from javascript

Posted by Ramani Sandeep on April 7, 2009

I thought that updating an UpdatePanel from javascript was one of the most frequently asked question about ASP.NET Ajax, but even looking around a lot I didn’t find a clean and polished solution for what should be a common task.

after few minutes of Googl-ing i found a useful link. View it for more details

Click Here for more details…

Posted in ASP.NET Ajax, JavaScript | Tagged: | Leave a Comment »

Set Focus on dropdown list after Partial Postback

Posted by Ramani Sandeep on November 28, 2008

——————————————————————-
Problem
——————————————————————-
I have a dropdownlilst that does a Partial postback when it’s index is changed.

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>

<ContentTemplate>

<asp:DropDownList ID=”ddlCountry” runat=”server” CssClass=”combobox” AutoPostBack=”true”

OnSelectedIndexChanged=”ddlCountry_SelectedIndexChanged” onblur=”check_ddlCountry()”

TabIndex=”5″>

</asp:DropDownList>

<asp:DropDownList ID=”ddlState” runat=”server” CssClass=”combobox” AutoPostBack=”true”

OnSelectedIndexChanged=”ddlState_SelectedIndexChanged” onblur=”check_ddlState()”

TabIndex=”6″>

</asp:DropDownList>

</ContentTemplate>

</asp:UpdatePanel>

After it does the Partial postback, it loses focus and when I click tab, I start from the item that

is TabIndex=”1″. I would like to set focus back to this item.

———————————————-
Solution 1
———————————————-

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);

if (CountryID > 0)
{
FillState(CountryID);
}

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), “FocusOnState”,

“document.getElementById(‘” + ddlState.ClientID + “‘).focus(); “, true);

}

———————————————-
Solution 2
———————————————-
If Your page is AJAX enabled page and you have ScriptManager on the page, then you can use this code.

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);

if (CountryID > 0)
{
FillState(CountryID);
}

ScriptManager manager = ScriptManager.GetCurrent(this);
manager.SetFocus(“ddlState”);

}

Both Solution Work for me. Hope this help you also..

Thanks, Enjoy Programming.

Posted in ASP.NET Ajax | Tagged: , , | 3 Comments »

File Upload Control is not working in Update Panel

Posted by Ramani Sandeep on November 22, 2008

The essence of the problem is that the FileUpload control does not work with asynchronous postbacks, and therefore does not work from within an AJAX UpdatePanel.

The technique presented in this article allows the FileUpload control to work within an UpdatePanel, by forcing a full postback; however, the rest of the controls can still take advantage of the asynchronous postbacks provided by the UpdatePanel.

Solution
————

The trick is to force the file upload control to perform a full postback, and we do this using triggers. Triggers allow the developer to specify what will cause partial and full postbacks. They must be defined within the UpdatePanel but outside of the ContentTemplate. We want to create a trigger that will instruct the button that we are using for the upload to perform a full postback.

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server” UpdateMode=”conditional”>
<Triggers>
<asp:PostBackTrigger ControlID=”Button1″ />
</Triggers>
<ContentTemplate>

<asp:FileUpload ID=”FileUpload1″ runat=”server” />
<asp:Button ID=”Button1″ runat=”server” Text=”Upload” OnClick=”Button1_Click” />

</ContentTemplate>
</asp:UpdatePanel>

Posted in ASP.NET, ASP.NET Ajax | Tagged: , , | 6 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 317 other followers