Ros*_*ied 10 html forms asp.net
我有一个标准的aspx页面,我需要在其中添加另一个标准HTML表单并将其提交到另一个位置(外部站点),但是每当我按下提交按钮时,页面似乎都会回复而不是使用子页面形成行动网址.
模拟下面的表单关系.请注意,在实际部署中,表单将成为母版页布局的内容区域的一部分,因此表单需要从母版页表单独立提交.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<form id="subscribe_form" method="post" action="https://someothersite.com" name="em_subscribe_form" >
<input type="text" id="field1" name="field1" />
<input id="submitsubform" type="submit" value="Submit" />
</form>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
bre*_*dan 10
这是一个有趣的问题.理想情况下,您只需要其他用户提到的页面上的1表单标记.您可以通过javascript发布数据而无需2个表单标记.
从这里取的示例,根据您的需要进行了修改.不是100%肯定这是否适合你,但我认为这是你必须接近它的方式.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function postdata()
{
var fieldValue = document.getElementById("field1").value;
postwith("http://someothersite.com",{field1:fieldValue});
}
function postwith (to,p) {
var myForm = document.createElement("form");
myForm.method="post" ;
myForm.action = to ;
for (var k in p) {
var myInput = document.createElement("input") ;
myInput.setAttribute("name", k) ;
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput) ;
}
document.body.appendChild(myForm) ;
myForm.submit() ;
document.body.removeChild(myForm) ;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<input type="text" id="field1" name="field1" />
<asp:Button ID="btnSubmitSubscribe" runat="server" Text="Submit" OnClientClick="postdata(); return false;" />
</div>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果javascript不是一个可行的选项 - 你可以使用.Net的HttpWebRequest对象在后面的代码中创建post调用.在后面的代码中看起来像这样(假设你的文本字段是一个asp文本框:
private void OnSubscribeClick(object sender, System.EventArgs e)
{
string field1 = Field1.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="field1="+field1 ;
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://someotherwebsite/");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
对于这个问题,有一个非常好的棘手解决方案.
您可以</form>
在<form>
关闭导致问题的asp.net表单之前插入标记.不要忘记<form>
在html表单后添加标记.它可能会导致编辑器给你一个例外,但不要担心,它会起作用.
归档时间: |
|
查看次数: |
18859 次 |
最近记录: |