Using HTTP POST

2 views
Skip to first unread message

Phil Sandler

unread,
Jun 19, 2007, 11:41:02 AM6/19/07
to PageMethods
All,

I can't quite figure out how to get HTTP POST working with
PageMethods. The GET works fine.

I have added the code to the global.asax page, and verified that it
gets called. I have set EnableViewStateMac="false" on both the
calling and target pages.

My setup is a gridview with a select button on each row. When the
select button is clicked, I want to navigate to the edit page from the
code behind. I started with:

(onSelectedIndexChanged)
long attId =
Convert.ToInt64(GridView1.SelectedDataKey.Value.ToString());
Response.Redirect(MyPageMethods.CustomAttributeEdit.EditCustomAttribute_Post(attId,
this));

I wasn't sure what to use for the second parameter, since no control
is directly causing the redirect (it's happening from the code behind
really). Anyway, I changed it to this:

long attId =
Convert.ToInt64(GridView1.SelectedDataKey.Value.ToString());
_hidden.Value =
MyPageMethods.CustomAttributeEdit.EditCustomAttribute_GetPostData(attId);
Response.Redirect(MyPageMethods.CustomAttributeEdit.EditCustomAttribute_Post(attId,
_hidden));

where the _hidden is a hdden field on my form. The GetPostData method
correctly populates the value of the hidden field.

I then realized that the EditCustomAttribute_Post method returns a
javascript function block.

Do I need to register the client script as a startup script and then
register another script on the client side to call it?

Is it not possible to do POSTs directly from the code behind? Should
I be using hyperlinks with click events instead?


Thanks for any insight,

Phil

Fabrice Marguerie

unread,
Jun 26, 2007, 8:10:07 AM6/26/07
to PageMethods
Hello Phil,

You do not have to use the _Post method for what you are trying to do.
All you need is a URL to redirect to. This is why you can simply use
the following code:
Response.Redirect(MyPageMethods.CustomAttributeEdit.EditCustomAttribute(attId));

You'd use the _Post method if you wanted to use a page method with a
button (or a LinkButton). In this case, the browser would HTTP post
directly to the target page without doing a postback to the current
page, as it is the case with your code.
Please refer to the samples to see how to use HTTP POST with
PageMethods.

The best solution I'd recommend when you need to link to pages without
postback is to simply use hyperlinks. This feels more natural for the
web.
You can see how to implement this in a grid by taking a look at Step 4
of the tutorial: http://metasapiens.com/PageMethods/Tutorial/VS2005/

Regards,
Fabrice

Reply all
Reply to author
Forward
0 new messages