名称'txtcategory '在当前上下文中不存在

本文关键字:上下文 不存在 txtcategory 名称 | 更新日期: 2023-09-27 18:05:08

. NET Web Forms

基本上,我在这段代码中要做的是获取用户输入的类别名称(使用txtcategorname),然后保存它,并在下面显示创建的所有类别(使用lvcategories)

下面是我的aspx文件

<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="Categories.aspx.cs" Inherits="Categories" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <!-- Page-Level Plugin CSS - Tables -->
    <link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet" />
    <link href="css/panel-nav.css" rel="stylesheet" />
    <link href="bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet" />
    <link href="css/bootstrap-switch.min.css" rel="stylesheet" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphPage" runat="Server">

    <h3 class="page-header">Categories<asp:Label runat="server" ID="CategoryDetails"></asp:Label></h3>

    <div class="panel panel-default">
                <div class="panel-heading">
                    Administrative Actions
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <!-- Button trigger modal -->
                    <button class="btn btn-primary " style="" onclick="return false;" data-toggle="modal" data-target="#addSection">
                        Create Category
                    </button>
                    <!-- Modal -->
                    <div class="modal fade" id="addSection" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">Categories <i class="fa fa-caret-right"></i>Add Category</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="form-group input-group" style="width: 100%;">
                                        <label class="control-label">Category Name</label>
                                        <asp:TextBox runat="server" ID="txtCategName" CssClass="form-control" ></asp:TextBox>
                                        </div>
                                    </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <asp:Button runat="server" ID="btnSubmit" CssClass="btn btn-primary" Text="Save changes" OnClick="btnSubmit_Click"></asp:Button>
                                </div>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                        </div>
                        <!-- /.modal -->

                        <asp:ListView runat="server" ID="lvPageTabs">
                            <LayoutTemplate>
                                <ul class="nav nav-tabs" id="draggableTab">
                                    <li runat="server" id="itemPlaceholder"></li>
                                </ul>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <li class="tab-heading">
                                    <a href='<%# "#"+ Eval("name").ToString().ToLower().Replace(" ", "-") %>' data-toggle="tab"><%# Eval("name") %> </a>
                                </li>
                            </ItemTemplate>
                        </asp:ListView>
                    </div>
                </div>



    <asp:ListView ID="lvCategs" OnItemDeleting="lvCategs_ItemDeleting" OnItemCommand="lvCategs_ItemCommand" DataKeyNames="category_id" runat="server">
                <LayoutTemplate>
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Categories Created
                        </div>
                        <div class="table table-bordered">
                            <table class="table table-hover">
                                <thead runat="server">
                                    <tr>
                                        <th>Date Created</th>
                                        <th>Category Name</th>
                                        <th>Edit/Delete Brand</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <td runat="server" id="itemPlaceholder"></td>
                                </tbody>
                            </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lblCategoryDateCreated" runat="server"
                                Text='<%#Eval("date_created") %>' /></td>
                        <td>
                            <asp:Label ID="lblCategoryName" runat="server"
                                Text='<%#Eval("name") %>' /></td>

                        <td>
                            <asp:Button runat="server" CssClass="btn btn-default" CommandName="Edit" ID="btnEdit" Text="Edit" />
                            <asp:Button runat="server" CssClass="btn btn-danger" CommandName="Delete" ID="btnDelete" Text="Delete" />

                        </td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>
    <!-- Page-Level Plugin Scripts - Tables -->
    <script src="bootstrap3-editable/js/bootstrap-editable.js"></script>
    <script src="js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>
    <script src="js/bootstrap-switch.min.js"></script>
</asp:Content>

这里是代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Categories : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DisplayCategories();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        using (Entities db = new Entities())
        {
            Category categ = new Category();
            categ.name = txtCategName.Text;
            categ.date_created = DateTime.Now;
            db.Categories.Add(categ);
            db.SaveChanges();
            DisplayCategories();
        }
    }
    protected void DisplayCategories()
    {
        using (Entities db = new Entities())
        {
            List<Category> categs = db.Categories.ToList();
            lvCategs.DataSource = categs.OrderByDescending(c => c.date_created);
            lvCategs.DataBind();
        }
    }
    protected void lvCategs_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            using (Entities db = new Entities())
            {
                int id = Convert.ToInt32(lvCategs.DataKeys[e.Item.DisplayIndex].Values["category_id"]);
                Category catg = db.Categories.FirstOrDefault(b => b.category_id == id);
                db.Categories.Remove(catg);
                db.SaveChanges();
                DisplayCategories();
            }
        }
    }
    protected void lvCategs_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
    }
}

虽然代码工作得很好,但每当我构建解决方案时,它仍然给我错误The name 'txtCategName' does not exist in the current contextThe name 'lvCategs' does not exist in the current context

我复制了代码并创建了一个新文件,正如其他一些解决方案所建议的,但它仍然不起作用。

名称'txtcategory '在当前上下文中不存在

将类重命名为_Categories等待智能感知提示它应该要求您将类重命名回Categories

这基本上只是一个Visual Studio的错误,它失去了你的标记和后面的代码之间的联系。强制它给你一个实际的错误,它将强制智能感知刷新并再次注意连接