Question On Viewstate and ButtonClicks

  • Thread starter Thread starter OHM
  • Start date Start date
O

OHM

Hi,

I'n new to ASP.NET and I have a question maybe someone could answer for me.

I realise that viewstate holds information about the state of the controls
such as the values of text boxes etc.

However, If I have two buttons on a form, how does ASP.NET know which one I
clicked on as they both submit. It cannot be the viewstate which is held
in a hidden field because this is generate when the page is output on
request or postback, so what is it that tells the ASP.NET that button1 was
pressed rather than button2.
I assume that something in the Request object does this, but what ?

TIA
 
OHM said:
Hi,

I'n new to ASP.NET and I have a question maybe someone could answer for me.

I realise that viewstate holds information about the state of the controls
such as the values of text boxes etc.

However, If I have two buttons on a form, how does ASP.NET know which one I
clicked on as they both submit. It cannot be the viewstate which is held
in a hidden field because this is generate when the page is output on
request or postback, so what is it that tells the ASP.NET that button1 was
pressed rather than button2.
I assume that something in the Request object does this, but what ?

TIA

The name of the button is transmitted to the page.
You can test this:

<html>
<body>
<form method="get" action="http://localhost/test.aspx">
<input type="submit" name="submitFirst" value="first" />
<input type="submit" name="submitSecond" value="second" />
</form>
</body>
</html>

method="get" to be able to see the name value pairs that are posted to
the server in the query string. If the method="post" the name value
pairs are hidden.
Notice the query string when you press the first and the second button
 
Thanks, is this visible in the clas hierarchy, in other words, can this be
determined prgamatically, or do we simply see this in terms of the events
fired.

Cheers - OHM
 
Actually, I found it, its in the following class instance

Request.form.AllKeys
 
Hi OHM,

Yes, in addition to using the different name to distinguish the buttons
,when the page be postedback (because of the button submit events),
there'll exist a certain item in the Request.Forms collection which
indicate which button cause the submit post back. Generally this is
apparent to the developers since ASP.NET runtime will handle it and
determine which event handler be called. But if you have interests , you
can manually loop it and found it. :)
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top