Webform post handling

  • Thread starter Thread starter Eagle Rock
  • Start date Start date
E

Eagle Rock

Hi all,

This is probably a simple question, but I'm just getting started in ASP.NET,
and I can't find the answer.

I have a simple form with a "Submit" button. When the user clicks on the
button, I want to post the data in the field to a URL, which is, of course,
very simple. But I also want it to send me an email message.

I know how to do either ONE of those things, but not both. I'm working in
C#. Is there some "post" command I can use in the button handling code that
will do this?

Thanks,
Dan
 
Hi Eagle:

Is the Submit button an ASP.NET server control? I.e, if you look at
the code view for the ASPX page, you should see:

<asp:Button runat="server" ... />

and not an HTML <input> tag.

If you use a server side button (just drop one on the web form from
design view), ASP.NET will automatically post back to the same form
when the user clicks the button. You can add an event handler for the
button by double clicking on the button in design view. From inside
the event handler you could add the little bit of code to send off an
email.

HTH,
 
Hi Scott,

Thanks, but I'm still not clear what to do.

For example -- if I have the following HTML form, the data is sent to the indicated web site and interpreted, but no mail message is sent.

<form method="post" action="https://www.someURL.com/processor.cfm">
<input type="hidden" name="var1" value="First Value">
<input type="hidden" name="var2" value="Second Value">
<input type="hidden" name="var3" value="Third Value">
<input type="submit" value="Submit">
</form>

If I make it an ASP.NET server control with a handler, I think it would look something like this:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
MailMessage mailMsg = new MailMessage();

mailMsg.From = (e-mail address removed);
mailMsg.To = (e-mail address removed);
mailMsg.Subject = "Order Form";
mailMsg.Body = "First Value. Second Value. Third Value";

SmtpMail.Send( mailMsg );

// But I don't know what to put here to post the form data to
// https://www.someURL.com/processor.cfm
...
}

Thanks,
Dan
 
Hi Dan:

First, I might be confused about where you are posting. Are you
posting a form back to the same application? Your application?

If so, you could create a web form similar to the following (notice
runat="server" on the input) :

<form id="Form1" method="post" runat="server">
<INPUT id="Hidden1" type="hidden" name="Hidden1" runat="server">
<asp:Button id="Button1" runat="server"
Text="Button">
</asp:Button>
</form>

And the code behind something like:

public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string s = Hidden1.Value;
// send email, etc.
}
}

If you are posting to another application, you might want to check all
the form variables to make sure the names are correct. Perhaps the
other server is doing additional validation with cookies or by looking
at the referer header to make sure the POST is coming from someone who
has been to their application.

Hope this is making more sense for you,
 
Hi Scott,

Thanks for your help.

The page I want to implement is a confirmation of a customer's order just
BEFORE the order is placed. When they click the "Submit" button on this
confirmation page, two things have to happen --
(1) The page has to send me an email with the information about the order,
and
(2) It has to post some information to a DIFFERENT web site, which will
process their credit card information.

The following static HTML code works just fine to do part number (2), i.e.,
post the action to process the credit card information. But it does nothing
to send me an email about the details of the order. So clearly I have to
use ASP.NET rather than static HTML

<form method="post" action="https://www.someURL.com/processor.cfm">
<input type="hidden" name="var1" value="First Value">
<input type="hidden" name="var2" value="Second Value">
<input type="hidden" name="var3" value="Third Value">
<input type="submit" value="Submit">
</form>

Supposing I was using your ASP.NET code below, and can get the email part
working -- what can I put in method "Button1_Click" that would do the same
thing as the 6 lines above -- i.e., submit the form data to some other URL,
for example https://www.someURL.com/processor.cfm.

Thanks,
Dan
 
Hi Dan:

Ah, I see now. Yes, it's quite possible to POST to another server from
inside the click event. You'll need to use the WebRequest class from
the System.Net namespace. It can make HTTP requests (both GET and
POST) to another web server.

I have a few links handy:

Look at ClientPost and ClientGetWithSSL here:
http://msdn.microsoft.com/library/d...tart/html/cpsmpnetsamples-howtonetworking.asp

Screen Scraping, ViewState, and Authentication using ASP.Net
http://odetocode.com/Articles/162.aspx

I know from experience, however, it can be tricky to get just right
and debug. The server will expect all the POSTed values in just the
right format. It's even harder over SSL because there are no tools
that can snoop the traffic and watch what is being passed.


hth,
 
Hi Scott,

Thanks for the help. I'll have to look into those web sites.

I'm also wondering if the "Response.Redirect" method might be the right
vehicle, if I can attach the Response name/value pairs.

Thanks,
Dan
 
Back
Top