views:

318

answers:

3

Currently I have two pages:

The first page contains an input form, and the 2nd page generates an excel document. The input form's button posts to this 2nd page.

What I'd like to do is add a second button which also posts to the 2nd page; however, I'll need requests created from this new button to act differently, which brings me to my question:

Is there a way I can tell, from the 2nd page, which button was pressed to submit the request?

The main reason I'm asking is I'd like to re-use the 2nd page's logic in parsing the information from the first page if possible; I'd rather not have to copy it to a new page and have the new button post to that.

Thanks!

A: 

If this is just pure HTML and your form action attribute points to the 2nd page...

You simply need to have two different submit buttons with the same name attrib and two different value attribs. When either button is clicked, it'll submit the form fields to the second page. Checking the value of the posted element with the submit button's name will tell you which one was clicked.

If we're talking ASP.NET (as your title and tag suggests) you can handle the server-side click event of each button in a postback to determine which one was clicked. But submitting an ASP.NET form to a second page isn't necessarily simple. See the Scott Guthrie post on URL Rewriting, specifically the section near the end about overriding the form tag's action attrib.

jtalarico
At the moment, it's setup so that the first page has no real logic; the button has its "PostBackUrl" attribute set to the 2nd page, where the 2nd page uses 'Page.PreviousPage' to get the values of what was posted
John
A: 

After doing some debugging, it looks like my best bet is to determine whether or not the button is included in the form values.

I.e.

if(Request.Form["Button1ClientID"] != null)
  //button 1 stuff
else if(Request.Form["Button2ClientID"] != null)
  //button 2 stuff

If anyone knows of a better way to do this, please let me know

Edit: UniqueID, not ClientID

John
+1  A: 

You could have a hidden text box on the form from the first page that each button sets a value in before posting to the second page. The second page could then evaluate the value of that hidden text box.

Edit: After re-reading your post, I think I misunderstood what you were attempting to accomplish. If you're simply trying to determine which button on the sending page was clicked, it could be done with the querystring:

Page1.aspx:

<asp:Button ID="Button1" Text="Button 1" PostBackUrl="~/Page2.aspx?button=1" runat="server" />
<asp:Button ID="Button2" Text="Button 2" PostBackUrl="~/Page2.aspx?button=2" runat="server" />

Page2.aspx.cs:

string sButton = "0";
if (Request.QueryString["button"] != null)
    sButton = Request.QueryString["button"];

if (sButton == "1")
    // Do button 1 stuff
else if (sButton == "2")
    // Do button 2 stuff
CAbbott
Nice. (15 characters)
John