首页上一页 1 下一页尾页 2 条记录 1/1页
大量报错“上下文中不存在名称XXX”
发表在ASP.NET图书答疑
2016-07-29
是否精华
是
否
版块置顶:
是
否
光盘中的代码复制到电脑里,直接拖到新建项目里。有大量报错 当前上下文中不存在名称XXX,但是这些控件是有的,也能正常运行。这是什么原因呢?百度了一些类似问题但是没看明白怎么改。
【ASP.NET开发实战1200例 源文件588】
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DBClass obj = new DBClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDLBind();
}
}
//绑定普通用户名
public void DDLBind()
{
SqlConnection myConn = obj.GetConnection();
myConn.Open();
string sqlStr = "select EmpId,EmpName from Tb_EmpInfo";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "EmpInfo");
DDLUserName.DataSource = ds.Tables["EmpInfo"].DefaultView;
DDLUserName.DataTextField = ds.Tables["EmpInfo"].Columns[1].ToString();
DDLUserName.DataValueField = ds.Tables["EmpInfo"].Columns[0].ToString();
DDLUserName.DataBind();
DDLUserName.Items.Insert(0, "请选择...");
da.Dispose();
ds.Dispose();
myConn.Close();
}
//绑定系统管理员
public void DDLMangerBind()
{
SqlConnection myConn = obj.GetConnection();
myConn.Open();
string sqlStr = "select MangerId,MangerName from Tb_Manger";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "EmpInfo");
DDLUserName.DataSource = ds.Tables["EmpInfo"].DefaultView;
DDLUserName.DataTextField = ds.Tables["EmpInfo"].Columns[1].ToString();
DDLUserName.DataValueField = ds.Tables["EmpInfo"].Columns[0].ToString();
DDLUserName.DataBind();
DDLUserName.Items.Insert(0, "请选择...");
da.Dispose();
ds.Dispose();
myConn.Close();
}
protected void btnLoad_Click(object sender, EventArgs e)
{
if (DDLUserName.SelectedIndex == 0)
Response.Write("<script>alert('请选择用户名!')</script>");
else
{
if (DDLGrade.SelectedIndex == 0)
{
int intValue = obj.IsValueUser(DDLUserName.SelectedItem.Text.Trim(), txtPassword.Text.Trim());
if (intValue == 100)
{
Session["EmpId"] = Convert.ToInt32(DDLUserName.SelectedValue.ToString());
Response.Redirect("~/Frame/Index.aspx");
}
else
Response.Write("<script>alert('您输入有误,请重新输入!')</script>");
}
else
{
int intValue = obj.IsValueManger(DDLUserName.SelectedItem.Text.Trim(), txtPassword.Text.Trim());
if (intValue == 100)
{
Response.Redirect("~/Manger/Frame/Index.aspx");
}
else
Response.Write("<script>alert('您输入有误,请重新输入!')</script>");
}
}
}
protected void DDLGrade_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDLGrade.SelectedIndex == 0)
{
DDLBind();
}
else
{
DDLMangerBind();
}
}
}
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 bgcolor ="#ffffcc">
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<table align="center" bordercolor = "#003300" border =1>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="身份:"></asp:Label></td>
<td>
<asp:DropDownList ID="DDLGrade" runat="server" OnSelectedIndexChanged="DDLGrade_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Selected="True">普通用户</asp:ListItem>
<asp:ListItem>系统管理员</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="用户名:" ></asp:Label></td>
<td>
<asp:DropDownList ID="DDLUserName" runat="server">
</asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="密码:"></asp:Label></td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:Button ID="btnLoad" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Black"
Text="登录" OnClick="btnLoad_Click" /></td>
</tr>
</table>
</form>
</body>
</html>
【ASP.NET开发实战1200例 源文件588】
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DBClass obj = new DBClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDLBind();
}
}
//绑定普通用户名
public void DDLBind()
{
SqlConnection myConn = obj.GetConnection();
myConn.Open();
string sqlStr = "select EmpId,EmpName from Tb_EmpInfo";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "EmpInfo");
DDLUserName.DataSource = ds.Tables["EmpInfo"].DefaultView;
DDLUserName.DataTextField = ds.Tables["EmpInfo"].Columns[1].ToString();
DDLUserName.DataValueField = ds.Tables["EmpInfo"].Columns[0].ToString();
DDLUserName.DataBind();
DDLUserName.Items.Insert(0, "请选择...");
da.Dispose();
ds.Dispose();
myConn.Close();
}
//绑定系统管理员
public void DDLMangerBind()
{
SqlConnection myConn = obj.GetConnection();
myConn.Open();
string sqlStr = "select MangerId,MangerName from Tb_Manger";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "EmpInfo");
DDLUserName.DataSource = ds.Tables["EmpInfo"].DefaultView;
DDLUserName.DataTextField = ds.Tables["EmpInfo"].Columns[1].ToString();
DDLUserName.DataValueField = ds.Tables["EmpInfo"].Columns[0].ToString();
DDLUserName.DataBind();
DDLUserName.Items.Insert(0, "请选择...");
da.Dispose();
ds.Dispose();
myConn.Close();
}
protected void btnLoad_Click(object sender, EventArgs e)
{
if (DDLUserName.SelectedIndex == 0)
Response.Write("<script>alert('请选择用户名!')</script>");
else
{
if (DDLGrade.SelectedIndex == 0)
{
int intValue = obj.IsValueUser(DDLUserName.SelectedItem.Text.Trim(), txtPassword.Text.Trim());
if (intValue == 100)
{
Session["EmpId"] = Convert.ToInt32(DDLUserName.SelectedValue.ToString());
Response.Redirect("~/Frame/Index.aspx");
}
else
Response.Write("<script>alert('您输入有误,请重新输入!')</script>");
}
else
{
int intValue = obj.IsValueManger(DDLUserName.SelectedItem.Text.Trim(), txtPassword.Text.Trim());
if (intValue == 100)
{
Response.Redirect("~/Manger/Frame/Index.aspx");
}
else
Response.Write("<script>alert('您输入有误,请重新输入!')</script>");
}
}
}
protected void DDLGrade_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDLGrade.SelectedIndex == 0)
{
DDLBind();
}
else
{
DDLMangerBind();
}
}
}
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 bgcolor ="#ffffcc">
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<table align="center" bordercolor = "#003300" border =1>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="身份:"></asp:Label></td>
<td>
<asp:DropDownList ID="DDLGrade" runat="server" OnSelectedIndexChanged="DDLGrade_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Selected="True">普通用户</asp:ListItem>
<asp:ListItem>系统管理员</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="用户名:" ></asp:Label></td>
<td>
<asp:DropDownList ID="DDLUserName" runat="server">
</asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Red"
Text="密码:"></asp:Label></td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:Button ID="btnLoad" runat="server" Font-Bold="True" Font-Names="隶书" ForeColor="Black"
Text="登录" OnClick="btnLoad_Click" /></td>
</tr>
</table>
</form>
</body>
</html>