使用与ASP.NET嵌入标准的HTML表单 [英] Using embedded standard HTML forms with ASP.NET

查看:79
本文介绍了使用与ASP.NET嵌入标准的HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要添加其他标准的HTML形式进入,并将它提交到另一个位置(外部网站),但每当我preSS提交按钮的页面似乎做回发一个标准的aspx页面而不是使用子表单操作URL。

一个实物模型的形式关系,下面是什么。请注意,在实际部署的形式将是一个母版页布局的内容区域的一部分,所以形式需要从母版页形式提交independantly

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
       <头=服务器>
          <标题>无标题页< /标题>
       < /头>
       <身体GT;
           <表ID =form1的=服务器>
           < D​​IV>
               <表ID =subscribe_form方法=邮报行动=htt​​ps://someothersite.comNAME =em_subscribe_form>
                    <输入类型=文本ID =字段1NAME =字段1/>
                    <输入ID =submitsubform类型=提交值=提交/>
               < /表及GT;
           < / DIV>
           < /表及GT;
       < /身体GT;
   < / HTML>


解决方案

这是一个有趣的问题。理想情况下,你只希望其他用户所提到的页面上的1表单标签。可能你能后通过JavaScript的数据,而不必2表单标签。

例如,从<一个取href=\"http://mentaljetsam.word$p$pss.com/2008/06/02/using-javascript-to-post-data-between-pages/\">here,修改您的需求。不是100%肯定这是否会为你工作,但我认为你这是怎么得接近它。

 &LT; HTML的xmlns =htt​​p://www.w3.org/1999/xhtml&GT;
&LT;头=服务器&GT;
&LT;标题&GT;无标题页&LT; /标题&GT;
&LT;脚本类型=文/ JavaScript的&GT;功能POSTDATA()
{
   。VAR fieldValue方法=的document.getElementById(字段1)值;
   postwith(http://someothersite.com,{field1的:fieldValue方法});
}功能postwith(到,P){
  VAR myForm会=使用document.createElement(表);
  myForm.method =邮报;
  myForm.action =到;
  为(P中VAR K){
    VAR myInput =使用document.createElement(输入);
    myInput.setAttribute(姓名,k)的;
    myInput.setAttribute(价值,P [K]);
    myForm.appendChild(myInput);
  }
  document.body.appendChild(myForm会);
  myForm.submit();
  document.body.removeChild(myForm会);
}&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
&LT;表ID =form1的=服务器&GT;
&LT; D​​IV&GT;
           &LT; D​​IV&GT;
                &LT;输入类型=文本ID =字段1NAME =字段1/&GT;
                &LT; ASP:按钮的ID =btnSubmitSubscribe=服务器文本=提交的OnClientClick =POSTDATA();返回false; /&GT;       &LT; / DIV&GT;&LT; / DIV&GT;
&LT; /表及GT;
&LT; /身体GT;
&LT; / HTML&GT;

如果JavaScript是不是一个可行的选择 - 您可以使用NET的HttpWebRequest对象后面创建code后调用。看起来像这背后(假设你的文本字段中的code是一个asp文本框:

 私人无效OnSubscribeClick(对象发件人,发送System.EventArgs)
{
字符串字段1 = Field1.Text;
ASCIIEncoding编码=新ASCIIEncoding();
字符串POSTDATA =字段1 =+字段1;
字节[]数据= encoding.GetBytes(POSTDATA);// prepare网页的请求......
HttpWebRequest的myRequest =
  (HttpWebRequest的)WebRequest.Create(HTTP:// someotherwebsite /);
myRequest.Method =POST;
myRequest.ContentType =应用/的X WWW的形式urlen codeD;
myRequest.ContentLength = data.Length;
方通流= myRequest.GetRequestStream();
//发送数据。
newStream.Write(数据,0,data.Length);
newStream.Close();
}

I have a standard aspx page with which I need to add another standard HTML form into and have it submit to another location (external site), however whenever I press the submit button the page seems to do a post back rather than using the sub-forms action url.

A mock up of what the form relationships is below. Note in the real deployment the form will be part of a content area of a master page layout, so the form needs to submit independantly from the master page form.

    <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>

解决方案

It's an interesting problem. Ideally you only want the 1 form tag on the page as other users have mentioned. Potentially you could post the data via javascript without having 2 form tags.

Example taken from here, modified for your needs. Not 100% sure if this will work for you but I think this is how you'll have to approach it.

<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>

If javascript is not a viable option - you can use .Net's HttpWebRequest object to create the post call in code behind. Would look something like this in the code behind (assuming your text field is an asp textbox:

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();
}

这篇关于使用与ASP.NET嵌入标准的HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆