views:

127

answers:

1

I'm new to ASP.NET and have a form tag on an ascx user control. I'm unable to submit the form from javascript because I found out that this form is nested inside a form called 'aspnetForm'. If I just want to make a post to a cgi, how can I accomplish this?

+4  A: 

Remove the <form runat='server'> if you don't need it and just use your own form: <form action="page.cgi" method="post">. You'll not be able to use some server controls. Use their HTML equivalents instead.

If you don't have control on the page, you can use javascript to inject a new form on the page with some hidden fields and set the values upon click of a button.

Something like this:

var myForm = document.createElement("form");
myForm.attributes["action"] = "mycgi.cgi";
myForm.attributes["method"] = "POST";
var myhiddenfield = document.createElement("input");
myhiddenfield.attributes["type"] = "hidden";
myhiddenfield.attributes["name"] = "name"
document.body.appendChild(myForm);
myForm.appendChild(myhiddenfield);

function onFormButtonClick() { // set as onclick on a <button>
    myhiddenfield.value = ... //value read from a textbox or something.
    ...
    myForm.submit();
}
Mehrdad Afshari
I don't have runat='server' attribute in the form tag. It is simply in in the .ascx page and is a control loaded by a webpart so it wraps aspnetForm around it and therefore I'm unable to submit.
LB
Oh, so you don't have control on the actual page. You can use javascript to inject a form on the page and submit that instead.
Mehrdad Afshari
how do u do that ? it appears that the form will always be nested inside the aspnetForm form.
LB
@LB -- you can see in his script example that the form is appended to the body. It isn't inside the aspnetForm. Note that you'll need to copy the values of the inputs from your control to hidden fields on the dynamically created form. On the server side these are the fields you'll read.
tvanfosson
Hmm... for some reason I'm unable to add the form... it doensn't even hit an alert('test'); after the 2nd line....
LB