Файл Code Behind не распознает имена элементов управления, несмотря на их наличие на странице дизайнера - PullRequest
0 голосов
/ 09 октября 2019

Мой компьютер умер за выходные, и мои резервные копии не удалось. Я создавал сайт ASP.NET C #, поэтому, естественно, все файлы исчезли. Я повторно загрузил Visual Studio, а также все файлы моего сайта с веб-сервера. Единственные вещи, которые этого не сделали, были .designer.cs files.. Здесь возникают проблемы. Ни один из моих кодов за страницами не распознает имена элементов управления на его родительской странице.

  • StackOverflow сказал мне запустить Project > Convert to Web Application, так что я сделал. Это добавило страницу дизайнера, но не решило проблему.

  • Если я пытаюсь добавить новый элемент управления и получить доступ к его событию click, VS помещает код в тег <script>на родительской странице, а не в коде позади.

  • Все имена элементов управления являются правильными и присутствуют на новой странице конструктора.

Iпонятия не имею, почему эти страницы не соединяются друг с другом. Ниже мой код для одной из страниц. Они все похожи на этот. Большое спасибо за любую помощь.

Код для родительской страницы:

<%@ Page Title ="Calculator Demo" Language ="C#" AutoEventWireup="True"
    Inherits="CalculatorDemo.aspx.cs" Codebehind="CalculatorDemo.aspx.cs" %>

<!DOCTYPE html>
<script runat="server">  
</script>    

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td colspan="3">
                        <asp:Label ID="lblResultLabel" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="txtValueBox1" runat="server"
                            OnTextChanged="txtValueBox1_TextChanged">
                        </asp:TextBox>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlOperatorList" runat="server">
                            <asp:ListItem>+</asp:ListItem>
                            <asp:ListItem>-</asp:ListItem>
                            <asp:ListItem>*</asp:ListItem>
                            <asp:ListItem>/</asp:ListItem>
                        </asp:DropDownList>
                        <asp:TextBox ID="txtValueBox2" runat="server">
                        </asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="btnCalculate" runat="server" 
                            OnClick="btnCalculate_Click" Text="Calculate" />
                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Код для кода позади:

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

public partial class Demos_CalculatorDemo : BasePage
{       
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        if (txtValueBox1.Text.Length > 0 && txtValueBox2.Text.Length > 0)
        {
            double result = 0;
            double value1 = Convert.ToDouble(txtValueBox1.Text);
            double value2 = Convert.ToDouble(txtValueBox2.Text);

            Calculator myCalculator = new Calculator();

            switch (ddlOperatorList.SelectedValue)
            {
                case "+":
                    result = myCalculator.Add(value1, value2);
                    break;
                case "-":
                    result = myCalculator.Subtract(value1, value2);
                    break;
                case "*":
                    result = myCalculator.Multiply(value1, value2);
                    break;
                case "/":
                    result = myCalculator.Divide(value1, value2);
                    break;
            }

            lblResultLabel.Text = result.ToString();
        }
        else
        {
            lblResultLabel.Text = string.Empty;
        }
    }
}

Код для страницы дизайнера:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace CalculatorDemo.aspx
{       
    public partial class cs
    {    
        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;

        /// <summary>
        /// lblResultLabel control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Label lblResultLabel;

        /// <summary>
        /// txtValueBox1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox txtValueBox1;

        /// <summary>
        /// ddlOperatorList control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.DropDownList ddlOperatorList;

        /// <summary>
        /// txtValueBox2 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox txtValueBox2;

        /// <summary>
        /// btnCalculate control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button btnCalculate;
    }
}
...