Soroush Hakami
Soroush Hakami

Reputation: 5406

Passing parameter from .aspx.cs to .ashx

I want to pass a string to a .ashx page.

Normally I'll do it by setting a parameter in the .aspx page like for example: Loader="TreeLoader.ashx?passedVariable=hello"

But I want to do it programmatically on the .aspx.cs side because the value will change.

The .ashx page accepts a HTTPContext:

public void ProcessRequest(HttpContext context)
    {

Shouldn't there be some way to add a parameter to the context, and then get the parameter in a way similar to this:

string searchString = context.Request["searchString"];

What is the best way to achieve this?

Upvotes: 3

Views: 18513

Answers (2)

VinayC
VinayC

Reputation: 49225

The correct way will depend upon how the control is passed to the ashx from the aspx file. If the handler is called from server side (using Server.Transfer method) then you can use the context object itself. For example, in aspx.c file

HttpContext.Current["key"] = data;
Server.Transfer("TreeLoader.ashx");

And in ashx file

public void ProcessRequest(HttpContext context)
{
    var data = context["key"];
    ...

Advantage being you can pass the actual object as data (and not necessarily a string).

If call will be made from client (browser) side then you need to pass data as query string parameter - such as TreeLoader.ashx?searchString=data and use it in ashx as context.Request["searchString"].

Upvotes: 6

BreakHead
BreakHead

Reputation: 10672

How are you calling tree handler from .aspx.cs? I think it will be same as 'TreeLoader.ashx?passedVariable=hello' for eg: Response.Redirect("TreeLoader.ashx?passedVariable=hello").

Upvotes: 0

Related Questions