1

I always got this error:Compiler Error Message: CS0103: The name 'txtUserName' does not exist in the current context

mypage.aspx

<% @Page Language="C#" Inherits="myclass" Src="myclass.cs" %> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>

</td>
</tr>

<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="Writedata" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

myclass.cs

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

Any help here?

Thanks,

2
  • 1
    Is this a webapplication or website project? However, the page directive does not contain a codebehind. <%@ Page Language="cs" AutoEventWireup="false" CodeBehind="myclass.aspx.cs" Inherits="Namespace.myclass" title="myclass-title" %> Commented Oct 9, 2012 at 13:12
  • it is a website project. Commented Oct 9, 2012 at 13:29

1 Answer 1

1

Try changing your class declaration to "public partial class myclass:System.Web.UI.Page". The partial keyword is, I believe, critical for the compiler to know the balance of the class definition is created in temp/intermediate files it won't know about otherwise.

Sign up to request clarification or add additional context in comments.

1 Comment

Add the "codefile="myclass.cs" to the Page directive in your aspx file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.