title: Passing textbox value to ASP.NET class
body: <p>I always got this error:Compiler Error Message: CS0103: The name 'txtUserName' does not exist in the current context</p>
  
  <p>mypage.aspx</p>
  
  <pre><code>&lt;% @Page Language="C#" Inherits="myclass" Src="myclass.cs" %&gt; 
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;Login Form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;
  Username:
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:TextBox ID="txtUserName" runat="server"&gt;&lt;/asp:TextBox&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="Writedata" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h1>myclass.cs</h1>
  
  <pre><code>using System;
  using System.Data;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.IO;
  using System.Net;
  using System.Text;
  
  public class myclass:Page 
  {
      protected void WriteData(object sender, EventArgs e){
  
      string customer_id=txtUserName.Text;
  
  
  
      string postData="customer_id="+customer_id;
  
      byte[] data= Encoding.UTF8.GetBytes (postData);
  
      // Prepare web request...
      HttpWebRequest myRequest =
          (HttpWebRequest)WebRequest.Create("http://myserverurl.com/mypage.php");
      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();
      }
  }
  </code></pre>
  
  <p>Any help here?</p>
  
  <p>Thanks,</p>