How does asp.net page know it's postback?

R

Rudy

Hi,
I need to make a request to a asp.net page using HttpWebRequest class
and I need to trick this page into thinking it's a postback and a
button was clicked. But the page doesn't handle the click event,
despite I'm using "POST" method and sending all the form fields
(except of __VIEWSTATE, may this be the reason ?).

So the question is: on what basis does asp.net page set the ispostback
property to true or false?

Regards,
 
J

John Saunders

Rudy said:
Hi,
I need to make a request to a asp.net page using HttpWebRequest class
and I need to trick this page into thinking it's a postback and a
button was clicked. But the page doesn't handle the click event,
despite I'm using "POST" method and sending all the form fields
(except of __VIEWSTATE, may this be the reason ?).

So the question is: on what basis does asp.net page set the ispostback
property to true or false?

I don't know, but I do know that you need to send ViewState. You need to
pretend to be a browser and do what a browser would have done.

John Saunders
 
P

Patrice

First, check that the postback property is correct...
IMO it's rather that it uses hidden fields that you need to fill to be able
to pretend to have clicked a button (__eventargument,__eventtarget)

It would likely cleaner/easier to have the owner of this page exposing a
programmatic only interface (your code could break after a change in the
UI). It's likely you'll have also some problems with the viewstate.

Patrice
 
K

Kevin Spencer

Rather than trying to break the object model, try working with it.

What I mean by that is, you don't need to make the page think it's a
PostBack when it isn't, and you don't need to make the page think a button
has been clicked when it hasn't. If you look at your requirements, what you
want is:

1. Make a Request to an ASP.Net page.
2. Make the ASP.Net page execute a Method when it is Requested in the way
you want to request it.

The fact that you have a button's OnClick event handler executing the same
code makes no difference whatsoever. Just remove the code from the event
handler and put it into a Method. Have the event handler call the Method.
Now, you can call the Method without an imaginary Click event.

All you need now is the logic that determines how and when the Method should
be fired.

I'm not sure why you're using a POST to the page, as you may think you need
it in order to set the "IsPostBack" property to true. However, assuming that
you need to send some data to the page, you can also send data via the form
fields in the page doing the POSTing, or via QueryString. The data can
include data that tells the page being called what it needs to do.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
B

bruce barker

asp.net checks for a valid viewstate stored in __ViewState. if it exists,
then its a postback. if you need to postback to an aspx. page from
webclient, you should do a "GET" first, parsre the html for the hidden field
__ViewState, then post this value back, along with the name of the button
you want to emulate a click, and any field data you want to post.

-- bruce (sqlwork.com)


| Hi,
| I need to make a request to a asp.net page using HttpWebRequest class
| and I need to trick this page into thinking it's a postback and a
| button was clicked. But the page doesn't handle the click event,
| despite I'm using "POST" method and sending all the form fields
| (except of __VIEWSTATE, may this be the reason ?).
|
| So the question is: on what basis does asp.net page set the ispostback
| property to true or false?
|
| Regards,
| --
| Rudy
 
R

Rudy

First, check that the postback property is correct...
IMO it's rather that it uses hidden fields that you need to fill to be able
to pretend to have clicked a button (__eventargument,__eventtarget)

ASP.NET uses __eventargument and __eventtarget only when the postback is
caused by something other than <input type="submit". I checked, that the
only hidden field sent after clicking the button, was __VIEWSTATE.
 
R

Rudy

asp.net checks for a valid viewstate stored in __ViewState. if it exists,
then its a postback. if you need to postback to an aspx. page from
webclient, you should do a "GET" first, parsre the html for the hidden field
__ViewState, then post this value back, along with the name of the button
you want to emulate a click, and any field data you want to post.
Yeah, I was afraid, I would have to do that :) Fortunately I have access
to the code executed during this postback and I can put it somewhere
else. Anyway, thanks for all the replies, everyone.
 
J

John Blair

Hi,

Wish i had the answer....I have the same problem - i can pick my data up
usiing requst.form("textbox1") say but no as textbox1.text i'd appreciate
it if you get an answer then e-mail me at (e-mail address removed)
Also if you know how to activate a button click via posting data i'd
appreciate knowing how to do that! Thanks a lot!
 
K

Karl Seguin

I don't have the original thread for this...but:

I believe ASP.Net can detect postback one of two ways...either via the
presence of __EventTarget or __ViewState..although the lalter is more
complicated than just simple existance check..

To simulate a button click in Javascript, use the __doPostBack function

<asp:button id="x" runat="server" />
<a href="javascript:__doPostBack('x', '');">hahah I'm sooo clever!</a>

If you only have Buttons and no LinkButtons or other controls which require
__doPostBack, you'll have to trick the page into generating the Javascript
for you (button's don't need javascript)...you can use
Page.GetPostBackClientEvent(x, ""); where x is the id of the button
control..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
J

Joseph Byrns

I don't know how to make the page thinks it's a postback, but as an
alternative could you call your page with a parameter then call the button
click from the page load, so:

http://www.asdsdfa.com/test.aspx?pb=true

then in the page load

if pb="true" then ButtonClickEvent(nothing,nothing)
 
T

Teemu Keiski

Yup,

basically detecting postback is at code level as follows:

<Browsable(False),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public ReadOnly Property IsPostBack As Boolean
Get
If (Me._requestValueCollection Is Nothing) Then
Return False
End If
Return Not Me._fPageLayoutChanged
End Get
End Property

The first one comes if there is a request value collection (form post
collection), if that's missing then it's not postback straight away. The
other one is detected if data coming from ViewState contains same hash code
as on previous request (first value in Page's ViewState is this hash code,
page's type hash code), that is if the hash code doesn't match then it is
not a postback (though there would be exception about invalid view state
before that :) )
 
J

John Blair

Thanks for the feedback - but i want to cause the event to activate as
part of the POST i.e. without changing the target aspx page (as i don't
control the source code there). I'm beginning to think this is not
possible as none of the examples ive come across using HTTPWebrequest
tackle this. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top