Using embedded standard HTML forms with ASP.NET

前端 未结 7 2026
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 11:54

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 sub

7条回答
  •  没有蜡笔的小新
    2020-12-14 12:37

    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.

    
    
    Untitled Page
    
    
    
    

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

提交回复
热议问题