大量报错“上下文中不存在名称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 />
        &nbsp;<br />
        <br />
        <br />
        &nbsp;</div>
        &nbsp; &nbsp;&nbsp;
        <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>

分享到:
精彩评论 2
慵懒阳光
学分:0 LV1
2016-08-03
沙发
[FIELDSET][LEGEND]引自:1楼[/LEGEND]
读者朋友:
    您好,提示***不存在,就是变量、控件或者命名空间没有引入引起的,建议在开发环境中双击每一处错误,然后定位到错误具体地方,然后选中,单击右键,选择“转到定义”,看看到底有没有声明。
[/FIELDSET]

回复:
  提示无法导航到定义。
aspx界面貌似是用html写的,比如这段
【aspx】
<tr>
                <td class="style1">
                    <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>
【.cs】
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, "请选择...");
这一段的 DDLUserName均有报错提醒:当前上下文中不存在名称“DDLUserName” ,这个应该声明什么吗?刚开始学不是很懂。。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照