1

我总是得到这个错误:编译器错误消息:CS0103:名称'txtUserName'在当前上下文中不存在

我的页面.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>

我的类.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();
    }
}

这里有什么帮助吗?

谢谢,

4

1 回答 1

1

尝试将您的类声明更改为“public partial class myclass:System.Web.UI.Page”。我相信,partial 关键字对于编译器了解类定义的平衡是在临时/中间文件中创建的至关重要,否则它不会知道。

于 2012-10-09T13:13:32.967 回答