我正在尝试从我正在构建的网站中获取真实的基本信息集,但它失败了,我无法弄清楚原因。
基础知识是我在aspx文件中有一个名为“txtUserName”的文本字段,所以我应该只能在aspx.cs文件中调用txtUserName.Text。但它表示当前上下文中不存在名称“txtUserName”。
这是代码。
Default.aspx有以下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
User Name:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ForeColor="Red" ID="RequiredFieldValidator2" ControlToValidate="txtUserName" runat="server" ErrorMessage="* Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPWD" ID="RequiredFieldValidator1" ForeColor="Red" runat="server" ErrorMessage="* Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" />
<asp:Button ID="bttnRegister" runat="server" Text="Register" onClick="btnRegister_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs有以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;
public partial class Default : System.Web.UI.Page
{
string myConnectionString = ConfigurationManager.ConnectionStrings["External"].ToString();
public SqlConnection createConnection()
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
myConnection.Open();
return myConnection;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtUserName.Text = "test";
}
答案 0 :(得分:1)
单击aspx上的右键,然后单击视图设计器。之后,txtUserName
应该在后面的代码中可见。有时会发生此错误,其他解决方案是手动将文本框添加到designer.cs文件中。