(悬赏20元)使用VS 2010开发asp.net/C#,但提示 错误 "_Default"不包含"txtName"的定义,该怎么处理
Default.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>页面跳转并传递参数</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="font-size: 9pt">
<tr>
<td style="width: 61px">
姓名:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 61px">
性别:</td>
<td style="width: 100px">
<asp:RadioButton ID="rbtnSex1" runat="server" Checked="True" Text="男" GroupName="Sex" />
<asp:RadioButton ID="rbtnSex2" runat="server" Text="女" GroupName="Sex" /></td>
</tr>
<tr>
<td style="width: 61px">
</td>
<td style="width: 100px">
<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="确定" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOK_Click(object sender, EventArgs e)
{
string name=this.txtName.Text;
string sex="先生";
if(rbtnSex2 .Checked)
sex="女士";
Response.Redirect("~/welcome.aspx?Name="+name+"&Sex="+sex);
}
}
welcome.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="welcome.aspx.cs" Inherits="welcome" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Welcome页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
welcome.aspx.cs代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.Params["Name"];
string sex = Request.Params["Sex"];
Response.Write("欢迎"+name+sex+"!");
}
}
CodeFile改CodeBehind试试。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
======>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>
CodeBehind
如果还是不行,改成无codeilfe模式的,就是新建aspx页面取消勾选将代码放到单独文件中,aspx直接内嵌代码
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOK_Click(object sender, EventArgs e)
{
string name=this.txtName.Text;
string sex="先生";
if(rbtnSex2 .Checked)
sex="女士";
Response.Redirect("~/welcome.aspx?Name="+name+"&Sex="+sex);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>页面跳转并传递参数</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="font-size: 9pt">
<tr>
<td style="width: 61px">
姓名:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 61px">
性别:</td>
<td style="width: 100px">
<asp:RadioButton ID="rbtnSex1" runat="server" Checked="True" Text="男" GroupName="Sex" />
<asp:RadioButton ID="rbtnSex2" runat="server" Text="女" GroupName="Sex" /></td>
</tr>
<tr>
<td style="width: 61px">
</td>
<td style="width: 100px">
<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="确定" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
vs2019按照你的代码建立aspx和cs文件没问题。之前碰到一个也是用2010开发导入别人的项目,运行没问题,但是vs就是提示无法找到控件。。不知道那里配置出问题了。。
txtName的TextBox控件相关的全部删掉,然后编译运行。没有错误后,再添加txtName的TextBox及其逻辑,然后再试。