Showing posts with label Asp.net-Ajax. Show all posts
Showing posts with label Asp.net-Ajax. Show all posts

Monday, January 4, 2010

Sys.StringBuilder Class in Ajax

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax_StringBuilder.aspx.cs" Inherits="Ajax_StringBuilder" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>

<script type="text/javascript">
function buildAString(title){
var headTagStart = "<head>";
var headTagEnd = "</head>";
var titleTagStart = "<title>";
var titleTagEnd = "</title>";

var sb = new Sys.StringBuilder(this._headTagStart);
Sys.Application.add_load(load);
sb.append(titleTagEnd);
sb.append(title);
sb.append(titleTagEnd);
sb.append(headTagEnd);
// Displays: "The result: <head><title>A Title</title></head>"
//alert("The result" + sb.toString());
}

var title = "A Title";
buildAString(title);
function load()
{

}
</script>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>

<%
Application.Add("Counter", 1);
Application["Counter"] = (int)Application["Counter"] + 1;
%>
</form>
</body>
</html>

Monday, December 14, 2009

ICallbackEventHandler Sample

.ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallBackEventValidationServer.aspx.cs" Inherits="CallBackEventHandler_CallBackEventValidationServer" %>

html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page<title>

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

function ReceiveServerData(rValue)

{

alert(rValue);

}

<script>

>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="sm" runat="server">asp:ScriptManager>

<div>

Name: <input type="text" id="Name" runat="server" />

Address: <input type="text" id="Address" runat="server" />

<input type="button" id="SUBMIT" value="SUBMIT" onclick="CallServer(1, alert('Callback'))" />

<div>

<form>

<body>

<html>



.CS Page

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

public partial class CallBackEventHandler_CallBackEventValidationServer : System.Web.UI.Page,ICallbackEventHandler

{

protected string returnValue = string.Empty;

protected void Page_Load(object sender, EventArgs e)

{

ClientScriptManager cm = Page.ClientScript;

String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData", "");

String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";

cm.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);

}

#region ICallbackEventHandler Members

public string GetCallbackResult()

{

return returnValue;

}

public void RaiseCallbackEvent(string eventArgument)

{

try

{

Page.ClientScript.ValidateEvent("SUBMIT");

// Callback logic goes here.

if (Name.Value == string.Empty)

{

returnValue += "* Please fill the value for Name \n";

}

if (Address.Value == string.Empty)

{

returnValue += "* Please fill the value for Address \n";

}

}

catch

{

// Failed callback validation logic.

}

}

#endregion

protected override void Render(HtmlTextWriter writer)

{

Page.ClientScript.RegisterForEventValidation("SUBMIT");

base.Render(writer);

}

}




Controls that Are Not Compatible with UpdatePanel Controls

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:

  • TreeView and Menu controls.

  • Web Parts controls. For more information, see ASP.NET Web Parts Pages.

  • FileUpload controls when they are used to upload files as part of an asynchronous postback.

  • GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

  • Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.

  • The Substitution control.

  • Validation controls, which includes the BaseCompareValidator, BaseValidator, CompareValidator, CustomValidator, RangeValidator, RegularExpressionValidator, RequiredFieldValidator, and ValidationSummary control.

Tuesday, November 24, 2009

PageRequestManager

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_pageLoaded(PageLoadedEventHandler);

function PageLoadedEventHandler()

{

alert("Inside PageLoadedEventHandler Working");

}

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest);

function InitializeRequest()

{

alert('Initial requets inside...');

}



By this way, we can fire all the Five ajax page life cycle events.

Ajax + asp.net Page life cycle

During ordinary page processing in the browser, the window.onload DOM event is raised when the page first loads. Similarly, the window.onunload DOM event is raised when the page is refreshed or when the user moves away from the page.

However, these events are not raised during asynchronous postbacks. To help you manage these types of events for asynchronous postbacks, the PageRequestManager class exposes a set of events. These resemble window.load and other DOM events, but they also occur during asynchronous postbacks. For each asynchronous postback, all page events in the PageRequestManager class are raised and any attached event handlers are called.

NoteNote:

For synchronous postbacks, only the pageLoaded event is raised.

You can write client script to handle events raised by the PageRequestManager class. Different event argument objects are passed to handlers for different events. The following table summarizes the PageRequestManager class events and the corresponding event argument classes. The order of the events in the table is the order of the events for a single asynchronous postback without errors.

initializeRequest

Raised before the request is initialized for an asynchronous postback. Event data is passed to handlers as an InitializeRequestEventArgs object. The object makes available the element that caused the postback and the underlying request object.

beginRequest

Raised just before the asynchronous postback is sent to the server. Event data is passed to handlers as a BeginRequestEventArgs object. The object makes available the element that caused the postback and the underlying request object.

pageLoading

Raised after the response to the most recent asynchronous postback has been received but before any updates to the page have been made. Event data is passed to handlers as a PageLoadingEventArgs object. The object makes available information about what panels will be deleted and updated as a result of the most recent asynchronous postback.

pageLoaded

Raised after page regions are updated after the most recent postback. Event data is passed to handlers as a PageLoadedEventArgs object. The object makes available information about what panels were created or updated. For synchronous postbacks, panels can only be created, but for asynchronous postbacks, panels can be both created and updated.

endRequest

Raised when request processing is finished. Event data is passed to handlers as an EndRequestEventArgs object. The object makes available information about errors that have occurred and whether the error was handled. It also makes available the response object.