动态地将表单添加到页面并在c#中提交。在IE浏览器中运行良好,但刚刚意识到它不能用于Chrome。我这是为我为一个小小的ecomerce应用程序构建的PayPal交易对象的功能。这是代码。
private void PayPalPostScriptLONG(Page page)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
if (UserID == string.Empty || EventID == string.Empty || TransactionType == string.Empty)
{
strScript.Append("alert('missing event, user, or transactiontype');");
}
else
{
strScript.AppendFormat("myform=document.createElement('form'); myform.method='post'; myform.target='_top'; myform.action='{0}'; myform.name='frmPP';", Paypal);
strScript.AppendFormat("input1=document.createElement('input'); input1.type='hidden'; input1.name='cmd'; input1.value='{0}'; myform.appendChild(input1);", Cmd);
strScript.AppendFormat("input2=document.createElement('input'); input2.type='hidden'; input2.name='business'; input2.value='{0}'; myform.appendChild(input2);", Business);
strScript.AppendFormat("input3=document.createElement('input'); input3.type='hidden'; input3.name='item_name'; input3.value='{0}'; myform.appendChild(input3);", Item_name);
strScript.AppendFormat("input4=document.createElement('input'); input4.type='hidden'; input4.name='amount'; input4.value='{0}'; myform.appendChild(input4);", Amount);
strScript.AppendFormat("input5=document.createElement('input'); input5.type='hidden'; input5.name='return'; input5.value='{0}'; myform.appendChild(input5);", ReturnURL);
strScript.AppendFormat("input6=document.createElement('input'); input6.type='hidden'; input6.name='custom'; input6.value='{0}'; myform.appendChild(input6);", Custom);
strScript.AppendFormat("input7=document.createElement('input'); input7.type='hidden'; input7.name='on0'; input7.value='uID'; myform.appendChild(input7);");
strScript.AppendFormat("input8=document.createElement('input'); input8.type='hidden'; input8.name='os0'; input8.value='{0}'; myform.appendChild(input8);", UserID);
strScript.AppendFormat("input9=document.createElement('input'); input9.type='hidden'; input9.name='on1'; input9.value='eID'; myform.appendChild(input9);");
strScript.AppendFormat("input10=document.createElement('input'); input10.type='hidden'; input10.name='os1'; input10.value='{0}'; myform.appendChild(input10);", EventID);
strScript.AppendFormat("input11=document.createElement('input'); input11.type='hidden'; input11.name='on2'; input11.value='type'; myform.appendChild(input11);");
strScript.AppendFormat("input12=document.createElement('input'); input12.type='hidden'; input12.name='os2'; input12.value='{0}'; myform.appendChild(input12);", TransactionType);
strScript.AppendFormat("input13=document.createElement('input'); input13.type='hidden'; input13.name='address1'; input13.value='{0}'; myform.appendChild(input13);", Address1);
strScript.AppendFormat("input14=document.createElement('input'); input14.type='hidden'; input14.name='address2'; input14.value='{0}'; myform.appendChild(input14);", Address2);
strScript.AppendFormat("input15=document.createElement('input'); input15.type='hidden'; input15.name='city'; input15.value='{0}'; myform.appendChild(input15);", City);
strScript.AppendFormat("input16=document.createElement('input'); input16.type='hidden'; input16.name='email'; input16.value='{0}'; myform.appendChild(input16);", Email);
strScript.AppendFormat("input17=document.createElement('input'); input17.type='hidden'; input17.name='first_name'; input17.value='{0}'; myform.appendChild(input17);", First_name);
strScript.AppendFormat("input18=document.createElement('input'); input18.type='hidden'; input18.name='last_name'; input18.value='{0}'; myform.appendChild(input18);", Last_name);
strScript.AppendFormat("input19=document.createElement('input'); input19.type='hidden'; input19.name='state'; input19.value='{0}'; myform.appendChild(input19);", State);
strScript.AppendFormat("input20=document.createElement('input'); input20.type='hidden'; input20.name='zip'; input20.value='{0}'; myform.appendChild(input20);", Zip);
strScript.AppendFormat("input21=document.createElement('input'); input21.type='hidden'; input21.name='night_ phone_a'; input21.value='{0}'; myform.appendChild(input21);", PhonePart1);
strScript.AppendFormat("input22=document.createElement('input'); input22.type='hidden'; input22.name='night_ phone_b'; input22.value='{0}'; myform.appendChild(input22);", PhonePart2);
strScript.AppendFormat("input23=document.createElement('input'); input23.type='hidden'; input23.name='night_ phone_c'; input23.value='{0}'; myform.appendChild(input23);", PhonePart3);
strScript.Append("document.appendChild(myform);");
strScript.Append("myform.submit();");
}
strScript.Append("</script>");
page.ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());
}
非常感谢任何帮助!!
谢谢!
我最终采用了jumpdarts建议并使用以下代码:
private string GetForm()
{
StringBuilder ppForm = new StringBuilder();
ppForm.AppendFormat("<Form name=\"frmPP\" id=\"frmPP\" action=\"{0}\" method=\"post\" target=\"_top\" \">", Paypal);
AddInput(ppForm,"cmd",Cmd);
AddInput(ppForm,"business",Business);
AddInput(ppForm,"item_name",Item_name);
AddInput(ppForm,"amount",Amount);
AddInput(ppForm,"return",ReturnURL);
AddInput(ppForm,"custom",Custom);
AddInput(ppForm,"on0",UserID);
AddInput(ppForm,"os0","uID");
AddInput(ppForm,"on1",EventID);
AddInput(ppForm,"os1","eID");
AddInput(ppForm,"on2",TransactionType);
AddInput(ppForm,"os2","type");
AddInput(ppForm,"address1",Address1);
AddInput(ppForm,"address2",Address2);
AddInput(ppForm,"city",City);
AddInput(ppForm,"email",Email);
AddInput(ppForm,"first_name",First_name);
AddInput(ppForm,"last_name",Last_name);
AddInput(ppForm,"state",State);
AddInput(ppForm,"zip",Zip);
AddInput(ppForm,"night_ phone_a",PhonePart1);
AddInput(ppForm,"night_ phone_b",PhonePart2);
AddInput(ppForm,"night_ phone_c",PhonePart3);
return ppForm.ToString();
}
private void AddInput(StringBuilder ppForm, string name, string value)
{
ppForm.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\">", name, value);
}
private void PayPalPostScriptLONG(Page page)
{
HttpContext.Current.Response.Write(GetForm());//add form to page
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
if (UserID == string.Empty || EventID == string.Empty || TransactionType == string.Empty)
{
strScript.Append("alert('missing event, user, or transactiontype');");
}
strScript.Append("var ctlForm = document.getElementById('frmPP');");
strScript.Append("ctlForm.submit();");
strScript.Append("</script>");
page.ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());
}
答案 0 :(得分:1)
不要这样做。将您的表单作为html文本写入您的响应,然后附加一个jscript函数找到它并提交它。
像这样:
Response.Write(<Form...);
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var daForm = document.getElementById('frmPP');");
strScript.Append("daForm.submit();");
strScript.Append("</script>");
ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());
答案 1 :(得分:0)
使用<script type="text/javascript">
!!