Mohsin Khan

Mohsin Khan

  • NA
  • 51
  • 1.1k

Return that bold line output

Feb 7 2019 5:21 AM
Please find below the C# code,
 
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
//Prepare the Posting form
string strForm = PreparePOSTForm(destinationUrl, data);
//Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(strForm)); -----> Getting error in this line
}private static String PreparePOSTForm(string url, NameValueCollection data)
{
//Set a name for the form
string formID = "RedirectForm";
//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
foreach (string key in data)
{
strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
}
strForm.Append("</form>");
//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var v" + formID + " = document." + formID + ";");
strScript.Append("v" + formID + ".submit();");
strScript.Append("</script>");
//Return the form and the script concatenated. (The order is important, Form then JavaScript)
return strForm.ToString() + strScript.ToString();
}
 
getting Error as An object reference is required for the non static field,method or property 'Control.Controls'

Answers (1)