html表单到C#代码隐藏代码

html表单到C#代码隐藏代码,c#,asp.net,forms,constantcontact,C#,Asp.net,Forms,Constantcontact,我正在尝试实现注册api的持续联系。他们给了我这个HTML代码。如果将其保存为HTML页面,则运行正常 我想做的是把这个表单转换成C代码,点击一个按钮就可以执行。HTML部分看起来不错,但今天是我使用ASP.net的第一天,我不知道在code behind.cs文件中放什么代码。搜索了很多,我非常困惑 单击“提交”按钮时单击此方法: protected void buttonId_Click(object sender, EventArgs e) { } 以下是我的ui端代码: <as

我正在尝试实现
注册
api的
持续联系
。他们给了我这个HTML代码。如果将其保存为HTML页面,则运行正常

我想做的是把这个表单转换成C代码,点击一个按钮就可以执行。HTML部分看起来不错,但今天是我使用ASP.net的第一天,我不知道在code behind.cs文件中放什么代码。搜索了很多,我非常困惑

单击“提交”按钮时单击此方法:

protected void buttonId_Click(object sender, EventArgs e)
{

}
以下是我的ui端代码:

<asp:TextBox ID="TextBox3" runat="server" class="name" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" class="name" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" class="name" value="Join our mailing list" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Join our mailing list';}"></asp:TextBox><br>

<asp:Button id="buttonId" OnClick="buttonId_Click" class="btn btn-info sub1" runat="server" Text="SUBSCRIBE">

</asp:Button>


以下是我从constant contact获得的HTML代码:

<form data-id="embedded_signup:form" class="ctct-custom-form Form" name="embedded_signup" method="POST" action="https://visitor2.constantcontact.com/api/signup">
   <p>Sign up to get interesting news and updates delivered to your inbox.</p>
   <!-- The following code must be included to ensure your sign-up form works properly. -->
   <input data-id="ca:input" type="hidden" name="ca" value="my-secrect-key">
   <input data-id="list:input" type="hidden" name="list" value="3">
   <input data-id="source:input" type="hidden" name="source" value="EFD">
   <input data-id="required:input" type="hidden" name="required" value="list,email">
   <input data-id="url:input" type="hidden" name="url" value="">
   <p data-id="Email Address:p" ><input data-id="Email Address:input" type="text" name="email" value="" maxlength="80"></p>
   <p data-id="First Name:p" ><input data-id="First Name:input" type="text" name="first_name" value="" maxlength="50"></p>
   <p data-id="Last Name:p" ><input data-id="Last Name:input" type="text" name="last_name" value="" maxlength="50"></p>
   <button type="submit" class="Button ctct-button Button--block Button-secondary" data-enabled="enabled">Sign Up</button>
</form>

注册以将有趣的新闻和更新发送到您的收件箱

注册
下面是一个可以放入按钮OnClick事件中的代码示例。将此帖子的数据发送到另一个URL

希望您能够了解这段代码中发生了什么。基本上,它是用HTML表单中的所有数据构建一个字符串(数据),并使用HTTP POST将其提交给其他网站

最有可能的情况是,您还需要查看其他网站的回复

string remoteUrl = "https://visitor2.constantcontact.com/api/signup";

ASCIIEncoding encoding = new ASCIIEncoding();
string data = "ca=my-secrect-key&list=3&source=EFD&required=list,email&url=&email=" + Server.UrlEncode(TextBox1.Text) + "&first_name=" + Server.UrlEncode(TextBox2.Text) + "&last_name=" + Server.UrlEncode(TextBox3.Text);
byte[] bytes = encoding.GetBytes(data);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = bytes.Length;
using (Stream stream = httpRequest.GetRequestStream())
{
    stream.Write(bytes, 0, bytes.Length);
    stream.Close();
}