By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,658 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,658 IT Pros & Developers. It's quick & easy.

Submit Form via code behind?

P: 2
I know I know, why would I want to submit the form from server-side code, if the only way to get to the server-side code was BY submitting the form. But that's just it, when the code-behind page_load event fires it doesn't really "submit" the form as I would like it submitted.

Here's my problem, I made a shopping cart for a website, and to get the information securely paid for I need to send a form to paypal using method="post" and action="big long url". In asp.net I can't have multiple forms with runat="server" on the same web page, so what I did was create a new page with the only form being the send-to-paypal form, and the page_load event handler in the codebehind populates the form tag with the correct transaction information (item names, prices, shipping, etc). Currently I have to have it create a button here too with type="submit". You have to click the button before the transaction is sent to paypal. Is there a way to make the Page_Load event fire the form's submit event so I don't have to require a button click?

Here's the html part of the new page with the form:

Expand|Select|Wrap|Line Numbers
  1. <div id="SendPaypalInfo" runat="server">
  2.             <FORM action="https://www.paypal.com/cgi-bin/webscr" method="post" runat="server"></FORM>
  3.         </div>
  4.  

and then here's the current Page_Load codebehind:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         If Not Page.IsPostBack Then
  3.             Dim TextToSend As String = ""
  4.             Dim a As Integer
  5.  
  6.             RetrieveSessionVariables()
  7.  
  8.             TextToSend = "<form name=""PaypalSubmitForm"" action=""https://www.paypal.com/cgi-bin/webscr"" method=""post"" runat=""server"">"
  9.             TextToSend = TextToSend & "<input type=""hidden"" name=""cmd"" value=""_cart"">"
  10.             TextToSend = TextToSend & "<input type=""hidden"" name=""business"" value=""EDITED"">"
  11.             TextToSend = TextToSend & "<input type=""hidden"" name=""upload"" value=""1"">"
  12. TextToSend = TextToSend & "<input type=""submit"" value=""Click Here""></form>"
  13.  
  14.  
  15.             Me.SendPaypalInfo.InnerHtml = TextToSend
  16.  
  17.         End If
  18.     End Sub

Can i submit the form in that very same page_load event? i don't want them to have to click that button :(
May 2 '06 #1
Share this Question
Share on Google+
16 Replies


P: 2
Can anyone help me out on this? :\


I was thinking, maybe I could put some kind of javascript on_load function in the body tag of my site, and it would be executed after asp creates the page's content, would that work? If so, how do i make a javascript on_load event :X
May 2 '06 #2

P: 8
Were you able to resolve this issue? If yes, how? I am looking for an answer as well !!!
Nov 13 '07 #3

10K+
P: 13,264
Were you able to resolve this issue? If yes, how? I am looking for an answer as well !!!
The details of this problem are quite strange. Submitting a form as the page loads means submitting a form without taking any user input at all which brings to question the reason for the existence of the form in the first place. It's possible of course to call a Javascript function onload of a page that submits a form on that page.
Nov 13 '07 #4

P: 8
Ok. My issue is bit different.
I have this form

<form name='test' id='test' runat='server'>
<input type='text' id='uName' name='uName'/>
<input type='text' id='pwd' name='pwd' />
<input type="submit" name="submit" ID="submit" value="submit" />
</form>

Now, my process is something like this:

1. Enter username, password and click 'Submit'
2. On the server side, do some authentication process (this I cannot do client side, because the process checks for something by sending a request to some server and receives the response)
3. If the response is valid, then submit this form.

Now, somebody tell me, how can I submit this form either using javascript function or through code-behind.
Nov 13 '07 #5

10K+
P: 13,264
Ok. My issue is bit different.
I have this form

<form name='test' id='test' runat='server'>
<input type='text' id='uName' name='uName'/>
<input type='text' id='pwd' name='pwd' />
<input type="submit" name="submit" ID="submit" value="submit" />
</form>

Now, my process is something like this:

1. Enter username, password and click 'Submit'
2. On the server side, do some authentication process (this I cannot do client side, because the process checks for something by sending a request to some server and receives the response)
3. If the response is valid, then submit this form.

Now, somebody tell me, how can I submit this form either using javascript function or through code-behind.
So on submit call a Javascript function which calls a server result through AJAX. You can then submit the form from that Javascript function based of the value of the returned result.
Nov 13 '07 #6

P: 8
That is exactly what I am trying to do. I am using AJAX to retrieve the response and trying to submit the form. Since I have runat="server" in the <form> tag, I am unable to submit the form through JavaScript function. See below.

<script language="javascript">
function submitf() //calling this function in onClick event of Submit button
{
Default.actuateAuthenticate(check_CallBack);
}

function check_CallBack(response)
{
if(response.value != "ERROR") //authentication works fine..
{
document.frmLogin.method = "post";
document.frmLogin.action = document.frmLogin.hdnTmpTarget.value;
document.frmLogin.submit();
}
}
</script>
Nov 13 '07 #7

10K+
P: 13,264
That is exactly what I am trying to do. I am using AJAX to retrieve the response and trying to submit the form. Since I have runat="server" in the <form> tag, I am unable to submit the form through JavaScript function. See below.

<script language="javascript">
function submitf() //calling this function in onClick event of Submit button
{
Default.actuateAuthenticate(check_CallBack);
}

function check_CallBack(response)
{
if(response.value != "ERROR") //authentication works fine..
{
document.frmLogin.method = "post";
document.frmLogin.action = document.frmLogin.hdnTmpTarget.value;
document.frmLogin.submit();
}
}
</script>
Any reason why the form should have the runat server tag in this case?
Nov 13 '07 #8

P: 8
If you don't specify runat="server", the AJAX method won't be executed at all. . It fails to call the method which is in the code behind.
Nov 13 '07 #9

10K+
P: 13,264
If you don't specify runat="server", the AJAX method won't be executed at all. . It fails to call the method which is in the code behind.
But of course! Sorry about that. My very bad.
Now you said it's not working. If you put an alert in your code to show
the value of document.frmLogin.hdnTmpTarget.value does it give the correct value?
Nov 13 '07 #10

P: 8
Yes. I am getting the value on alert. Only the submit part is not working. I believe it is because I have runat="server" in the form tag. But if I don't mention this, AJAX method won't be executed. This is like a deadlock for me now!!
Nov 13 '07 #11

10K+
P: 13,264
Yes. I am getting the value on alert. Only the submit part is not working. I believe it is because I have runat="server" in the form tag. But if I don't mention this, AJAX method won't be executed. This is like a deadlock for me now!!
How did you specify that the Javascript function should be called on the form?
Instead of having runat server call the javascript function onSubmit() of the form and see what happens.
Nov 13 '07 #12

P: 8
<form id="frmLogin" name="frmLogin" onsubmit="submitf();">

I tried this. But AJAX method didn't get executed. Because I need to have runat="server". If I mention this (as below), the form doesn't get submitted.

<form id="frmLogin" name="frmLogin" runat="server" onsubmit="submitf();">
Nov 13 '07 #13

10K+
P: 13,264
<form id="frmLogin" name="frmLogin" onsubmit="submitf();">

I tried this. But AJAX method didn't get executed. Because I need to have runat="server". If I mention this (as below), the form doesn't get submitted.

<form id="frmLogin" name="frmLogin" runat="server" onsubmit="submitf();">
That's strange. You certainly don't need the runat="server" control in order for you to be able to call an AJAX function as illustrated in the example here.
Nov 13 '07 #14

P: 8
Here, in this example, everything is done in client side javascript itself. Even the methods are defined in JS. But in my case, I have a public method which will be in server side when I execute the form and it is only available to the form when it runs on server.
Nov 13 '07 #15

10K+
P: 13,264
Here, in this example, everything is done in client side javascript itself. Even the methods are defined in JS. But in my case, I have a public method which will be in server side when I execute the form and it is only available to the form when it runs on server.
How's this workaround:
Submit the form before checking the value of the AJAX request then perform the check on the serverside code (you no longer need AJAX for that). An if else on the server side would bring the initial page or proceed with the processing. Of course this means that the page has to be submited ...
Nov 13 '07 #16

P: 8
I am not submitting the form to the same page. Once the AJAX method returns true value, the form gets submitted to another server mentioned in action attribute of the form tag.
Nov 13 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.