Saturday, 10 March 2012

How to Make Customise AJAX Request in Asp.net


var AjaxServerPageName = "AjaxServer.aspx";

function CreateXmlReq()
{
    try
    {
         XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
         try
         {
             XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(oc)
         {
             XmlReq = null;
         }
    }
    if(!XmlReq && typeof XMLHttpRequest != "undefined")
    {
         XmlReq = new XMLHttpRequest();
    }
}

function FetchDGContents(prod_id,ProdName,weight_id,Weight_value,ProdQty,Show_Amount,amt,TotalAmt,action,QtyId,Gmw_id,weight,Gm_w_id)
{
    debugger;
    var ProdId = prod_id;
    var P_Name = ProdName;
    var WeightId = weight_id.id;
    var WeightValue = Weight_value;
    var ProdAmt = amt;
    var TotAmt = TotalAmt;
    var ShowAmount = Show_Amount;
    var Trigger = action
    var Qty = ProdQty;
    var Qty_id = QtyId;
    var Gmwid = Gmw_id;
   
    var requestUrl = AjaxServerPageName + "?ProdId=" + encodeURIComponent(ProdId) + "&ProdName=" + encodeURIComponent(P_Name) + "&WeightId=" + encodeURIComponent(WeightId)+ "&WeightValue=" + encodeURIComponent(WeightValue)+ "&ProdAmt=" + encodeURIComponent(ProdAmt)+ "&TotAmt=" + encodeURIComponent(TotAmt)+ "&Trigger=" + encodeURIComponent(Trigger)+ "&ShowAmount=" + encodeURIComponent(ShowAmount)+ "&Qty=" + encodeURIComponent(Qty)+ "&Qty_id=" + encodeURIComponent(Qty_id)+ "&Gmwid=" + encodeURIComponent(Gmwid)+ "&weight=" + encodeURIComponent(weight)+ "&Gm_w_id=" + encodeURIComponent(Gm_w_id);
    CreateXmlReq();
    if(XmlReq)
    {
         XmlReq.onreadystatechange = HandleResponse;
         XmlReq.open("GET", requestUrl, true);
         XmlReq.send();       
    }
}

function HandleResponse()
{
    if(XmlReq.readyState == 4)
    {
         if(XmlReq.status == 200)
         {       
//           var tbl = document.getElementById('DataGrid1').getElementsByTagName("tbody")[0];   
////         // Before filling new contents into the DataGrid, clear the cotents by calling this function
////         ClearTable(tbl);
////         // Fill the cleared Datagrid with new XML Reponse
////         FillTable(XmlReq.responseXML.documentElement,tbl);
             // Hides the Process Image Table after displaying the contents
             //imgtbl.style.visibility = 'hidden';
//           alert('hello');
         }
         else
         {
             alert("There was a problem retrieving data from the server." );
         }
    }
}


Given Blow code should be in AjaxPage.aspx.vb/ C# file (as accordingly syntax)


#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub


        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Dim chString, choice As String
       
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

          If Not IsPostBack Then
           temptable()'should return any xml format 
            Else
                Response.Clear()
                Response.End()
            End If
  End Sub­­­­

Public Sub temptable()

Dim tabletest As New DataTable()

 Dim coltest As DataColumn = New DataColumn("Test")

 coltest.DataType = System.Type.GetType("System.String")
            tabletest.Columns.Add(coltest)
            Dim rowtest As DataRow = tabletest.NewRow()
            rowtest(coltest) = "Hello"
            tabletest.Rows.Add(rowtest)

            Dim ds As DataSet = New DataSet()

            ds.Tables.Add(tabletest)

            chString = ds.GetXml()
            ds.Clear()
            Response.Clear()
            Response.ContentType = "text/xml"
            Response.Write(chString)
            Response.End()
        End Sub

No comments:

Post a Comment