sukesh
sukesh

Reputation: 2523

How to get data from another page

The functionality exists in pageB.aspx and the parameters are passed from pageA. I'm failing to get the result of pageB. What should be done here.

pageA.aspx.cs:

string URI = Server.MapPath("~/pageB.aspx");
NameValueCollection UrlParams = new NameValueCollection();
UrlParams.Add("section", "10");
UrlParams.Add("position", "20");
using (WebClient client = new WebClient())
  {
    byte[] responsebytes = client.UploadValues(URI, "POST", UrlParams);
    string responsebody = Encoding.UTF8.GetString(responsebytes);
  }

when the compiler reads byte[], there is a dialog which asks if changes should be made to pageB. On clicking No, nothing happens. The string 'responsebody' is empty.

pageB.aspx.cs::

protected void Page_Load(object sender, EventArgs e)
    {
      if (int.TryParse(Request.QueryString["section"], out section)
                && int.TryParse(Request.QueryString["position"], out position))
            {
                ProcessData();
            }
    }
private string ProcessData()
    {
        string HTMLContent = string.Empty;
        try
        {
            //some code to render the string.                
            return HTMLContent;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I would like to get 'HTMLContent' from pageB

Upvotes: 1

Views: 190

Answers (3)

Dhrumil
Dhrumil

Reputation: 3204

Try using Session if you want to pass any type of data between different pages.

EDIT :

Since Session wont be working in your case, you could also try using WebCache instance to store information and access it across other pages.

This link might help here : MSDN : WebCache

Hope this helps.

Upvotes: 2

Ewan
Ewan

Reputation: 1285

Having pageA 'load' PageB via a web request is probably not the best idea in the world.

My reasoning for this is that I have seen it done on various projects and you can run into all sorts of problems with the routing of the request and permissions.

Essentially the PageA->PageB request will be treated like a new user loading PageB, cookies, session variables etc will be lost unless you explicitly populate them. firewall rules, routing, load balancing etc will act in odd ways.

Any of these problems could cause your unexpected behavior of PageA

Instead : Move the functionality out of pageB into a service object and call it directly from where needed.

However : Say for some reason you are required to do it via a web request. Perhaps its not always on the same server or something.

It looks from your sample code like you are treating PageB as a webservice. You pass in the parameters and use the whole response. So you could if required expose the underling code, lets call it ServiceB as a WebServiceB and then reference it from PageA via the web request.

This would be 'allowed' as the encapsulation of the data as a WebSerivce make it obvious that PageB is not a page of itself and should not rely on variables such as session, cookies etc which are not explicitly passed in.

Further more the calling code, 'knows' that the webservice could be on any machine in the world and thus won't expect it to know about things unless they are explicitly passed.

If you do change PageB to a service and you still need a stand alone PageB which returns HTML. Simply make a new PageB which calls ServiceB behind the scenes. This allows you to separate your 'view' logic from the data/logic provided by the service

Upvotes: 2

Keval Gangani
Keval Gangani

Reputation: 1328

Use HttpContext.Current.Request["Key"] instead of Request.QueryString["Key"].

if (int.TryParse(HttpContext.Current.Request["section"], out section)
            && int.TryParse(HttpContext.Current.Request["position"], out position))
{
     ProcessData();
}

Upvotes: 2

Related Questions