<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Development - Web Development Tutorial for Developer&#039;s, Asp.net Tutorial, PHP Development, Wordpress Integration &#187; AJAX</title>
	<atom:link href="http://www.solutions4ever.net/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solutions4ever.net</link>
	<description>Web Development Solutions Provider</description>
	<lastBuildDate>Fri, 03 Sep 2010 11:45:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET and Ajax &#8211; using XmlHttpRequest</title>
		<link>http://www.solutions4ever.net/2010/02/asp-net-and-ajax-using-xmlhttprequest/</link>
		<comments>http://www.solutions4ever.net/2010/02/asp-net-and-ajax-using-xmlhttprequest/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 07:03:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AJAX]]></category>

		<guid isPermaLink="false">http://www.solutions4ever.net/?p=94</guid>
		<description><![CDATA[<p>If you are not involved in using the ASP.NET AJAX library offered for ASP.NET, but would like to attribute small amounts of AJAX functionality on your pages, you can do this easily with some javascript and a accessible page.</p>
<p>I only use AJAX in small doses on web sites &#8211; mainly  ... <a href="http://www.solutions4ever.net/2010/02/asp-net-and-ajax-using-xmlhttprequest/" class="expert-read-more" >Read More</a>]]></description>
			<content:encoded><![CDATA[<p>If you are not involved in using the ASP.NET AJAX library offered for ASP.NET, but would like to attribute small amounts of AJAX functionality on your pages, you can do this easily with some javascript and a accessible page.</p>
<p>I only use AJAX in small doses on web sites &#8211; mainly to improve the user-experience. Typical instances will engage retrieving a full record based on a user’s selection in a drop down list. I don&#8217;t want to perform a full page post back, so I get the JavaScript xmlhttpserver object to do it for me behind the scenes. Here&#8217;s an example that fetches the address details for a North wind Traders customer.</p>
<p>First, a generic function to instantiate an instance of xmlhttprequest or xmlhttp, depending on the browser:</p>
<pre>  function GetXmlHttpObject(handler)</pre>
<pre>  {</pre>
<pre>  var objXmlHttp=null</pre>
<pre>  if (navigator.userAgent.indexOf("MSIE")&gt;=0)</pre>
<pre>  {</pre>
<pre>  var strName="Msxml2.XMLHTTP"</pre>
<pre>  if (navigator.appVersion.indexOf("MSIE 5.5")&gt;=0)</pre>
<pre>  {</pre>
<pre>  strName="Microsoft.XMLHTTP"</pre>
<pre>  }</pre>
<pre>  try</pre>
<pre>  {</pre>
<pre>  objXmlHttp=new ActiveXObject(strName)</pre>
<pre>  objXmlHttp.onreadystatechange=handler</pre>
<pre>  return objXmlHttp</pre>
<pre>  }</pre>
<pre>  catch(e)</pre>
<pre>  {</pre>
<pre>  alert("Error. Scripting for ActiveX might be disabled")</pre>
<pre>  return</pre>
<pre>  }</pre>
<pre>  }</pre>
<pre>  if (navigator.userAgent.indexOf("Mozilla")&gt;=0)</pre>
<pre>  {</pre>
<pre>  objXmlHttp=new XMLHttpRequest()</pre>
<pre>  objXmlHttp.onload=handler</pre>
<pre>  objXmlHttp.onerror=handler</pre>
<pre>  return objXmlHttp</pre>
<pre>  }</pre>
<pre>  }</pre>
<p>Now, a simple dropdown list populated by a SqlDataSource control that fetches the CustomerID and Customer Name. Note the empty div, CustomerDetails below the dropdown list:</p>
<pre>&lt;div&gt;</pre>
<pre>&lt;asp:DropDownList</pre>
<pre>runat="server"</pre>
<pre>DataSourceID="SqlDataSource1"</pre>
<pre>DataTextField="CompanyName"</pre>
<pre>DataValueField="CustomerID"&gt;</pre>
<pre>&lt;/asp:DropDownList&gt;</pre>
<pre> </pre>
<pre>&lt;asp:SqlDataSource</pre>
<pre>ID="SqlDataSource1"</pre>
<pre>runat="server"</pre>
<pre>ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb"</pre>
<pre>DataSourceMode="DataReader"</pre>
<pre>ProviderName="System.Data.OleDb"</pre>
<pre>SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]"&gt;</pre>
<pre>&lt;/asp:SqlDataSource&gt;</pre>
<pre> </pre>
<pre>&lt;/div&gt;</pre>
<pre>&lt;div&gt;&lt;/div&gt;</pre>
<p>Two more JavaScript functions are needed. One to fire the GetXmlHttpObject method, and denote the page to send a request to, and one to handle the response by checking the read State property of the object for a value of 4 or complete, then to write the response to the empty div:</p>
<pre>  function GetCustomer(id)</pre>
<pre>  {</pre>
<pre>  var url="FetchCustomer.aspx?CustomerID=" + id ;</pre>
<pre>  xmlHttp=GetXmlHttpObject(stateChanged);</pre>
<pre>  xmlHttp.open("GET", url , true);</pre>
<pre>  xmlHttp.send(null);</pre>
<pre>  }</pre>
<pre>  </pre>
<pre>  </pre>
<pre>  function stateChanged()</pre>
<pre>  {</pre>
<pre>  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")</pre>
<pre>  {</pre>
<pre>  document.getElementById('CustomerDetails').innerHTML=xmlHttp.responseText;</pre>
<pre>  }</pre>
<pre>  }</pre>
<p>So now we have a page that gets called by xmlhttp (FetchCustomer.aspx) and we have a value to pass to the query string for the page. So the next thing to do is to add an event handler to the Dropdown List that will fire the GetCustomer() event and pass a CustomerID value:</p>
<pre>  if (!Page.IsPostBack)</pre>
<pre>  {</pre>
<pre>  DropDownList1.DataBind();</pre>
<pre>  DropDownList1.Items.Insert(0, "");</pre>
<pre>  }</pre>
<pre> </pre>
<pre>  DropDownList1.Attributes["onChange"] = "GetCustomer(this.value);";</pre>
<pre>  HttpResponse myHttpResponse = Response;</pre>
<pre>  HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);</pre>
<pre>  DropDownList1.Attributes.AddAttributes(myHtmlTextWriter);</pre>
<pre> </pre>
<p>And finally, the code for the FetchCustomer.aspx page. Everything has been removed from the .aspx file except the first line. We don&#8217;t want DocTypes or default &lt;form&gt; tags disturbing the piece of html to be emitted in the response:</p>
<pre>&lt;%@ Page AutoEventWireup="true"</pre>
<pre>CodeFile="FetchCustomer.aspx.cs" Inherits="FetchCustomer" %&gt;</pre>
<pre> </pre>
<p>And the code-behind makes doubly sure by calling Response.Clear() before querying the database and emitting the resulting data as html:</p>
<pre>using System;</pre>
<pre>using System.Data;</pre>
<pre>using System.Data.OleDb;</pre>
<pre>using System.Text;</pre>
<pre> </pre>
<pre>public partial class FetchCustomer : System.Web.UI.Page</pre>
<pre>{</pre>
<pre>  protected void Page_Load(object sender, EventArgs e)</pre>
<pre>  {</pre>
<pre>    Response.Clear();</pre>
<pre>    StringBuilder sb = new StringBuilder();</pre>
<pre>    sb.Append("&lt;br /&gt;");</pre>
<pre>    string provider = "Provider=Microsoft.Jet.OLEDB.4.0;";</pre>
<pre>    string db = "Data Source=|DataDirectory|Northwind.mdb";</pre>
<pre>    string connstr = provider + db;</pre>
<pre>    string query = "SELECT * FROM Customers WHERE CustomerID = ?";</pre>
<pre>    OleDbConnection conn = new OleDbConnection(connstr);</pre>
<pre>    OleDbCommand cmd = new OleDbCommand(query, conn);</pre>
<pre>    cmd.Parameters.AddWithValue("", Request.QueryString["CustomerID"]);</pre>
<pre>    conn.Open();</pre>
<pre>    OleDbDataReader dr = cmd.ExecuteReader();</pre>
<pre>    while(dr.Read())</pre>
<pre>    {</pre>
<pre>      sb.Append(dr[1].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append(dr[4].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append(dr[5].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append(dr[6].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append(dr[7].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append(dr[8].ToString() + "&lt;br /&gt;");</pre>
<pre>      sb.Append("Tel: " + dr[9].ToString() + "&lt;br /&gt;");</pre>
<pre>    }</pre>
<pre>    dr.Close();</pre>
<pre>    dr.Dispose();</pre>
<pre>    conn.Close();</pre>
<pre>    conn.Dispose();</pre>
<pre>    Response.Write(sb.ToString())</pre>
<pre>    Response.End();</pre>
<pre>  }</pre>
<pre>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.solutions4ever.net/2010/02/asp-net-and-ajax-using-xmlhttprequest/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
