0

Below is the code which I am using to post form in ASP.NET to another page.....
It works fine as long as javascript is enabled, but stops on disabling javascript.

StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");
sb.AppendFormat("<form name='form' action='{0}' method='post'>",postbackUrl);
sb.AppendFormat("<input type='hidden' name='id' value='{0}'>", id);
// Other params go here
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");

Response.Write(sb.ToString());

Response.End();
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
user987166
  • 69
  • 3
  • There's an onLoad event handler embedded into the body tag, which is JavaScript (and can only be). If you turn JavaScript off, it *clearly* wont work. You can fix this by adding a button to submit the form, or, add a noscript tag to tell people that it wont work without JavaScript enabled. – SpaceBison Aug 31 '12 at 09:08

2 Answers2

3

Well, you're using javascript to post a form

onload='document.forms[""form""].submit()'

so when javascript is disabled your code won't execute. It's all working (or not working) as expected.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

THe reason why it doesn't work is because you are using JavaScript to submit the form.

"onload='document.forms[""form""].submit()'"

The code above is javascript.

You can alternatively add a submit button saying "Click to continue" for those users that don't have JavaScript enabled, but keep the OnLoad for users that have JavaScript enabled.

AndreCruz
  • 653
  • 4
  • 12
  • well the only problem is that i cant do that because it is being used in inner pages and cant show the page to the user – user987166 Aug 31 '12 at 09:12
  • what do you mean by inner page? Is it in an iFrame? Somehow the user must load the page, so you may need to work around and display a button to the user that doesn't support JavaScript. I don't think you can auto-submit a form without JavaScript, unless you force the user to press a button. It may require some redesign to allow for this scenario, or you may want to show an error message to users that do not have JavaScript enabled. – AndreCruz Aug 31 '12 at 09:20