Reden Rodriguez Ответов: 0

Имя "сетка" не существует в текущем контексте


It seems that my GridView ID(grid) and Label ID(result)  can't be detected on the code behind

but as you can see on my code behind it has the same name and capitalization and I already double checked the designer and also has the same name and capitalization


here are my codes
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    using System;
    using System.Data;
    using System.Web.UI.WebControls;

    namespace stupidcode
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    fill_grid();
                }
            }

            public void grid1_OnRowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlCategory = (DropDownList)e.Row.FindControl("ddlCategory");
                    if (ddlCategory != null)
                    {
                        ddlCategory.DataSource = Product.FetchCategory();
                        ddlCategory.DataBind();
                        ddlCategory.SelectedValue =
                            grid.DataKeys[e.Row.RowIndex].Values[1].ToString();
                    }
                }

                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    DropDownList ddlCategoryInput = (DropDownList)e.Row.FindControl(
                        "ddlCategoryInput");
                    ddlCategoryInput.DataSource = Product.FetchCategory();
                    ddlCategoryInput.DataBind();
                }
            }

            public void grid1_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
                TextBox txtProductNo = (TextBox)grid.Rows[e.RowIndex].FindControl("txtProductNo");
                TextBox txtColor = (TextBox)grid1.Rows[e.RowIndex].FindControl("txtColor");
                DropDownList ddlCategory = (DropDownList)grid.Rows[e.RowIndex].FindControl(
                    "ddlCategory");
                TextBox txtCost = (TextBox)grid.Rows[e.RowIndex].FindControl("txtCost");
                int upd = Product.update(grid.DataKeys[e.RowIndex].Values[0].ToString(),
                            txtName.Text,
                            txtProductNo.Text,
                            txtColor.Text,
                            ddlCategory.SelectedValue.ToString(),
                            txtCost.Text
                            );
                grid.EditIndex = -1;
                fill_grid();
                result.Text = upd.ToString() + " : Row updated";
            }

            protected void fill_grid()
            {
                DataTable dt = Product.FetchProducts();
                if (dt.Rows.Count > 0)
                {
                    DataView dv = new DataView(dt);
                    if (Session["sortexp"] != null)
                    {
                        string srtexpr = Session["sortexp"].ToString();
                        switch (srtexpr)
                        {
                            case "name":
                                dv.Sort = "name";
                                break;
                            case "color":
                                dv.Sort = "color";
                                break;
                            case "Category":
                                dv.Sort = "Category";
                                break;
                            case "Cost":
                                dv.Sort = "Cost";
                                break;
                            default:
                                grid.DataSource = dv;
                                break;
                        }
                    }
                    grid.DataSource = dv;
                    grid.DataBind();
                }
                else
                {
                    result.Text = " No rows Selected.";
                }
            }

            public void grid1_OnRowEditing(object sender, GridViewEditEventArgs e)
            {
                grid.EditIndex = e.NewEditIndex;
                fill_grid();
            }

            public void grid1_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                grid.EditIndex = -1;
                result.Text = " ";
                fill_grid();
            }

            public void grid1_OnRowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "Add_Rec")
                {
                    TextBox txtNameInput = (TextBox)grid.FooterRow.FindControl("txtNameInput");
                    TextBox txtProductNumberInput = (TextBox)grid.FooterRow.FindControl(
                        "txtProductNumberInput");
                    TextBox txtColorInput = (TextBox)grid.FooterRow.FindControl("txtColorInput");
                    DropDownList ddlCategoryInput = (DropDownList)grid.FooterRow.FindControl(
                        "ddlCategoryInput");
                    TextBox txtCostInput = (TextBox)grid.FooterRow.FindControl("txtCostInput");
                    int rowInsert
                        = Product.insertRow(
                                             txtNameInput.Text,
                                             txtProductNumberInput.Text,
                                             txtColorInput.Text,
                                             ddlCategoryInput.SelectedValue.ToString(),
                                             txtCostInput.Text
                        );
                    fill_grid();
                    result.Text = rowInsert.ToString() + " :Row was Inserted Successfully";
                }
            }

            public void grid1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                string product_Id = grid.DataKeys[e.RowIndex].Values[0].ToString();
                int rowdel = Product.RowDelete(product_Id);
                result.Text = rowdel.ToString() + " : Row was Deleted.";
                fill_grid();
            }

            public void grid1_OnSorting(object sender, GridViewSortEventArgs e)
            {
                string sortexp = e.SortExpression;
                Session["sortexp"] = sortexp;
                fill_grid();
            }

            public void grid1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                grid.PageIndex = e.NewPageIndex;
                fill_grid();
            }
        }
    }

<!-- language: lang-html -->

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="stupidcode.OngridSample" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <asp:GridView ID="grid" 
            runat="server" 
            AutoGenerateColumns="false"
            DataKeyNames="ProductId,Category"
            OnRowDataBound="grid1_OnRowDataBound"
            OnRowEditing="grid1_OnRowEditing"
            OnRowUpdating="grid1_OnRowUpdating"
            OnRowDeleting="grid1_OnRowDeleting"
            OnRowCommand="grid1_OnRowCommand"
            ShowFooter="true"
            OnRowCancelingEdit="grid1_OnRowCancelingEdit"
            AllowSorting="true"
            OnSorting="grid1_OnSorting"
            AllowPaging="true"
            PageSize="2"
            OnPageIndexChanging="grid1_OnPageIndexChanging"
            EmptyDataText="No records Selected." 
            BackColor="White" 
            BorderColor="#CCCCCC" 
            BorderStyle="None" 
            BorderWidth="1px" 
            CellPadding="4" 
            ForeColor="Black" 
            GridLines="Horizontal">
            <Columns>
                <asp:TemplateField HeaderText="Product Name" SortExpression="name">
                    <ItemTemplate>
                        <asp:Label ID="label1" runat="server" Text='<%#Eval("name") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNameInput" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Product No">
                    <ItemTemplate>
                        <asp:Label ID="label2" runat="server"
                            Text='<%#Eval("ProductNumber") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProductNo" runat="server"
                            Text='<%# Eval("ProductNumber") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtProductNumberInput" runat="server">
                        </asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Color" SortExpression="Color">
                    <ItemTemplate>
                        <asp:Label ID="label3" runat="server" Text='<%#Eval("Color") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtColor" runat="server"
                            Text='<%#Eval("Color") %>'>'</asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColorInput" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Category" SortExpression="Category">
                    <ItemTemplate>
                        <asp:Label ID="label4" runat="server"
                            Text='<%#Eval("Category") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlCategory" runat="server"
                            DataTextField="cat_id" DataValueField="cat_id">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlCategoryInput" runat="server"
                            DataTextField="cat_id" DataValueField="cat_id">
                        </asp:DropDownList>

                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Cost" SortExpression="Cost">
                    <ItemTemplate>
                        <asp:Label ID="label7" runat="server" Text='<%#Eval("Cost") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCost" runat="server"
                            Text='<%#Eval("Cost") %>'>'</asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtCostInput" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Edit" ShowHeader="false">
                    <EditItemTemplate>
                        <asp:LinkButton ID="linkButton1" runat="server"
                            CausesValidation="true"
                            Text="Update" CommandName="update"></asp:LinkButton>
                        <asp:LinkButton ID="linkButton2" runat="server"
                            CausesValidation="false"
                            Text="Cancel" CommandName="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="linkButton2" runat="server"
                            CausesValidation="false"
                            Text="Add Record" CommandName="Add_Rec"></asp:LinkButton>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="linkButton1" runat="server"
                            CausesValidation="false"
                            Text="Edit" CommandName="Edit"></asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:CommandField HeaderText="Delete" ShowDeleteButton="true"
                    ShowHeader="true" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black"></FooterStyle>

            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

            <PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

            <SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

            <SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

            <SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

            <SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
        </asp:GridView>
                    <br />
                    <br />
                    <asp:Label ID="result" runat="server" ForeColor="Blue"></asp:Label>
    </body>
    </html>


<!-- end snippet -->


Что я уже пробовал:

Я попытался изменить его serval time I doesn't work, а также поместить пространство имен моего кода позади в inherit it doesn't work.

F-ES Sitecore

Тег "наследует" на Вашей странице не соответствует классу в коде-позади, похоже, вы переименовывали вещи или перемещали вещи. Исправьте тег Inherits или, в качестве последнего результата, возьмите копию имеющихся у вас файлов, удалите страницу, затем создайте ее заново и верните элементы управления обратно, а код-обратно, оставив теги вверху, чтобы вы знали, что они правы.

0 Ответов