Very basic Java Script which require once in awhile in the world of ASP.NET.
I came across the situation where requirement was to post form to basic ASP page from asp.net page. That can be done by using PostBackUrl property available in Button control. Now that is simple but PostBackUrl will post entire ASP.NET form and extra data will be posted.
Complexity in requirement was that form should be posted to asp page to new window. I tried to find a lot to post data to new window using asp.net button control and PostBackUrl property. If you find please let me know. Then I had to write Java Script.
Ok. Now here is code after lots of text and description.
//Create form
function createForm(formName, actionURL, target, method)
{
var oForm = document.createElement("form");
oForm.name = formName; //Form name
oForm.id = formName; // Form ID
oForm.action = actionURL; // URL to post back
oForm.target = target; // This is important. Use “_blank” if you wish to post back to new window
oForm.method = method; // Method GET or POST
return oForm;
}
//Create input fields
function createInputField(name, value, type)
{
var oField = document.createElement("input");
oField.name = name;
oField.id = name;
oField.value = value;
oField.type = type; // Type normally “hidden”
return oField;
}
//Append Field to form
function appendField(oForm, oField)
{
oForm.appendChild(oField);
}
//Here is code to create form, field and submit the form
function submitEligibilityForm(sNewFormAction, Field1Value, Field2Value)
{
var oForm = createForm("FormName", sNewFormAction,"_blank","POST");
var oField = createInputField("Field1", Field1Value,"hidden");
appendField(oForm,oField);
oField = createInputField("Field2", Field2Value,"hidden");
appendField(oForm,oField);
document.appendChild(oForm); //This is required to add form to document
oForm.submit();
}
Here is HTML code to create and submit the form
<input tyep=”button” text=”click me” onlick=”submitToEligibilityHandoff('submitpage.asp',' Field2Value ', 'Field2Value'); return false;">