名称''在当前上下文中不存在 - 偶发

时间:2016-05-04 03:40:48

标签: c# asp.net visual-studio

我知道这是一个相当常见的错误,但在这种情况下我的情况有点不同。

有时,我不会得到这个错误,有时我会,这是不理想的。

通常我会更改inherit名称并将public partial class更改为相同,但工作正常,但过了一段时间我会收到以下内容。

Error   1   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 31  13  Branch(3)
Error   2   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 32  13  Branch(3)
Error   3   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 33  13  Branch(3)
Error   4   The name 'add_modules' does not exist in the current context    all-modules.aspx.cs 34  13  Branch(3)
Error   5   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 42  13  Branch(3)
Error   6   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 43  13  Branch(3)
Error   7   The name 'display_modules' does not exist in the current context    all-modules.aspx.cs 44  13  Branch(3)

以下是我的C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

public partial class all_modules: System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
  //Only allow admins, academic program managers and Senior University Managers to view page
  helper.Authorised(1, 2, 3);

  //get role_id of logged in user
  int role_id;
  role_id = Convert.ToInt32(Session["role_id"]);

  //get id of logged in user
  string user_id = Session["user_id"].ToString();

  //if admin
  if (role_id == 1 || role_id == 3) {
   string query = "SELECT courses.course_name, staff_records.f_name, staff_records.l_name, modules.module_name, modules.module_tutor, modules.module_id FROM courses_vs_modules INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN staff_records ON modules.module_tutor = staff_records.user_id INNER JOIN courses ON courses_vs_modules.course_id = courses.course_id WHERE (courses.school IN (SELECT school_id FROM staff_records AS staff_records_1 WHERE (user_id = @user_id))) ORDER BY courses.course_name";
   DataTable dt = GetData(query, user_id);
   display_modules.DataSource = dt;
   display_modules.DataBind();
   display_modules.Visible = true;
   add_modules.Visible = true;
  }

  //if senior uni manager
  if (role_id == 2) {
   string query = "SELECT courses.course_name, staff_records.f_name, staff_records.l_name, modules.module_name, modules.module_tutor, modules.module_id FROM courses_vs_modules INNER JOIN modules ON courses_vs_modules.module_id = modules.module_id INNER JOIN staff_records ON modules.module_tutor = staff_records.user_id INNER JOIN courses ON courses_vs_modules.course_id = courses.course_id ORDER BY courses.course_name";
   DataTable dt = GetDataSen(query);
   display_modules.DataSource = dt;
   display_modules.DataBind();
   display_modules.Visible = true;
  }

 }

 private static DataTable GetData(string query, string user_id) {

  DataTable dt = new DataTable();
  SqlCommand cmd = new SqlCommand(query);
  String constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection con = new SqlConnection(constr);
  SqlDataAdapter sda = new SqlDataAdapter();
  cmd.CommandType = CommandType.Text;
  cmd.Connection = con;
  sda.SelectCommand = cmd;
  cmd.Parameters.AddWithValue("@user_id", user_id);
  sda.Fill(dt);
  return dt;
 }

 private static DataTable GetDataSen(string query) {

  DataTable dt = new DataTable();
  SqlCommand cmd = new SqlCommand(query);
  String constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection con = new SqlConnection(constr);
  SqlDataAdapter sda = new SqlDataAdapter();
  cmd.CommandType = CommandType.Text;
  cmd.Connection = con;
  sda.SelectCommand = cmd;
  sda.Fill(dt);
  return dt;
 }
}

这是我的ASP.net

<%@ Page Title="All Modules" MasterPageFile="MasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="~/all-modules.aspx.cs" Inherits="all_modules" %>
<asp:Content ContentPlaceHolderID="head" Runat="Server">
    <script>
    $(document).ready(function () {
        //makes contains filter case insensitive
        $.expr[":"].contains = $.expr.createPseudo(function (arg) {
            return function (elem) {
                return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
            };
        });

        //search has the user types
        $('.search-box').keyup(function () {
            var search_term = $(this).val();
            if (search_term == "") {
                //show all if nothing was entered
                $('.project-link').removeClass('hide');
            } else {
                // hide all and then show only the search results
                $('.project-link').addClass('hide');
                $('.module-list *:contains("' + search_term + '")').closest('.project-link').removeClass('hide');
            }

            //check if any results were found
            if ($('.project-link').not('.hide').length)
            {
                $('.no-results').addClass('hide');

            } else {
                $('.no-results').removeClass('hide');
            }
        });

    });
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="menu" Runat="Server"></asp:Content>
<asp:Content ContentPlaceHolderID="main_content" Runat="Server">
    <div class="row">
        <div class="col-12">
            <h1>All Modules</h1>
            <h5 class="subheading">Search through a complete list of modules.</h5>
            <a href="add-modules.aspx" class="button blue" runat="server" id="add_modules" visible="false">Add Modules</a>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="panel">
                <h3>Search Modules:</h3>
                <input type="text" class="search-box full-width" placeholder="Search via module name, code, course or lecturer" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="panel">
                <asp:ListView ID="display_modules" runat="server" Visible="false">
                    <ItemTemplate>
                        <div class="project-link module-list">
                            <a href='<%# "module.aspx?module=" + Eval("module_id") %>'>
                                <p class="project-label"><%# Eval("course_name") %>
                                </p>
                                <asp:Label Text='
                                    <%# Eval("module_id") + " - " %>' runat="server" CssClass="story-title" ID="Label1" />
                                    <asp:Label Text='
                                        <%# Eval("module_name") %>' runat="server" CssClass="story-title" ID="story_titleLabel" />
                                        <span>
                                            <p class="project-label"><%# Eval("f_name") + " " + Eval("l_name") %>
                                            </p>
                                            <div class="to-module"></div>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:ListView>
                            <p class="hide no-results">No search results were found.</p>
                        </div>
                    </div>
                </div>
            </asp:Content>

知道为什么会这样吗?任何帮助将不胜感激

0 个答案:

没有答案