Friday, March 5, 2010

RemoteDataPass- pass data from one page to Remote Page

As i Committed in my previous post, that i will post code for ReportDataPass.

it is Quite difficult to pass Data from one Application to another application,
because here Cookie, Session variable will not help.
now a very simple way it Query string,  But as our previous condition, we don’t want to use Query string.
so the last way..
Post Method
as we know, in any server side scripting languages, like asp, jsp, PHP.. on submit button, the Data is posted to another page or to itself.
and using Request[“Filedname”]
we can have its Value. the same logic we are going to apply..
but as we are using VS, where we have facilities of Masterpages and Content page,so when we are using any ContentPage, in that case we can not have form and its Post method.

So we are going to make a dynamic Html Page, and  using dynamic Javascript we will post Data to another page.

So create a Class like this
   1: public class RemoteDataPass
   2: {
   3:     
   4:     private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
   5:     public string PostUrl = "";
   6:     public void Add(string FieldName, string Fieldvalue)
   7:     {
   8:         Inputs.Add(FieldName, Fieldvalue);
   9:     }
  10:  
  11:     public void Post()
  12:     {
  13:         System.Web.HttpContext.Current.Response.Clear();
  14:         System.Web.HttpContext.Current.Response.Write("<html><head></head><body onload=\"document.form1.submit()\">");
  15:         System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"form1\" method=\"post\" action=\"{0}\" >", PostUrl));
  16:         for (int i = 0; i < Inputs.Keys.Count; i++)
  17:         {
  18:             System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
  19:         }
  20:         System.Web.HttpContext.Current.Response.Write("</form></body></html>");        
  21:         System.Web.HttpContext.Current.Response.End();
  22:     }
  23: }








now to post Data from our Source Page.



   1: RemoteDataPass RemotePost = new RemoteDataPass();
   2:         RemotePost.PostUrl = "http://ransandeep.blogspot.com/test.aspx";
   3:         RemotePost.Add("FirstField", "Sandeep");        
   4:         RemotePost.Post(); 










now at Receiving page



   1: protected void Page_Load(object sender, EventArgs e)
   2:    {
   3:         if (Request.Form[ "FirstField" ] != null  )
   4:         {
   5:            Response.Write( "FirstField : "  + Request.Form[ "FirstField" ] +  "</br>" ) ;
   6:         }         
   7:    }







So this is Simple after getting know….





All the best..

No comments: