c# - 无法在代码隐藏中访问 Asp.net 控件

标签 c# asp.net

我有一个以前由其他人构建的项目。当我尝试向页面添加新控件时,我无法在后面的代码中访问它。当我尝试访问它时,出现一条错误消息,指出该控件未在当前上下文中定义。

<%@ Page Language="C#" MasterPageFile="~/HRMS.Master" AutoEventWireup="true"     CodeBehind="manageEmployee.aspx.cs" Inherits="HRMS.manageEmployee" Title="Untitled Page" %>

<%@ Register Assembly="eWorld.UI" Namespace="eWorld.UI" TagPrefix="ew" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
    document.getElementById("employee").setAttribute("class", "selected");
    </script>
<!--OPEN CONTENT DIV-->

     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
    <!-- Only this area is updated on PostBack -->

<div class="manage_employ">

    <ul>
        <li style="margin-top:10px; margin-bottom:15px"><input type="image" src="images/img_manageEmployee01.png"/></li>
    </ul>

    <ul>
        <li>First name</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtFirstName"/></li>
        <asp:RequiredFieldValidator ID="validatorFirstName" runat="server" ErrorMessage="*" ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>
        <li>Last name</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtLastName"/></li>
        <asp:RequiredFieldValidator ID="validatorLastname" runat="server" ErrorMessage="*" ControlToValidate="txtLastName"></asp:RequiredFieldValidator>
    </ul>

    <ul>
        <li>CNIC</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtCNIC"/></li>
        <asp:RequiredFieldValidator ID="validatorCNIC" runat="server" ErrorMessage="*" ControlToValidate="txtCNIC"></asp:RequiredFieldValidator>
        <li>Blood Group</li>
        <li><asp:DropDownList runat="server" class="text_input" id="ddlBloodGroup">
        <asp:ListItem>A+</asp:ListItem>
        <asp:ListItem>B+</asp:ListItem>
        <asp:ListItem>AB+</asp:ListItem>
        <asp:ListItem>A-</asp:ListItem>
        <asp:ListItem>B-</asp:ListItem>
        <asp:ListItem>AB-</asp:ListItem>
        <asp:ListItem>O+</asp:ListItem>
        <asp:ListItem>O-</asp:ListItem>
        </asp:DropDownList>
        </li>
    </ul>

    <ul>
        <li>Mobile</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtMobile"/></li>
        <asp:RequiredFieldValidator ID="validatorMobile" runat="server" ErrorMessage="*" ControlToValidate="txtMobile"></asp:RequiredFieldValidator>
        <li>Phone</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtPhone"/></li>
    </ul>

    <ul>


    </ul>

    <ul>
        <li>E-Mail</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtEmail"/></li>
        <asp:RegularExpressionValidator ID="valEmail" ControlToValidate="txtEmail" runat="server" ErrorMessage="*" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <li>Maritial Status</li>
            <li>
            <asp:RadioButton ID="rbtnMarried" AutoPostBack="true" runat="server" oncheckedchanged="rbtnMarried_CheckedChanged"/>Married
            <asp:RadioButton ID="rbtnUnmarried" AutoPostBack="true" runat="server" 
                    oncheckedchanged="rbtnUnmarried_CheckedChanged"/>Unmarried
            </li>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ul>

    <ul>
        <li>Permanant Address</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtPermanantAddress"/></li>
        <asp:RequiredFieldValidator ID="validatorPAddress" runat="server" ErrorMessage="*" ControlToValidate="txtPermanantAddress"></asp:RequiredFieldValidator>
        <li>Temporary Address</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtTemporaryAddress"/></li>
        <asp:RequiredFieldValidator ID="validatorTAddress" runat="server" ErrorMessage="*" ControlToValidate="txtTemporaryAddress"></asp:RequiredFieldValidator>
    </ul>


    <ul>
        <li>Salary</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtSalary"/></li>
        <asp:RequiredFieldValidator ID="validatorSalary" runat="server" ErrorMessage="*" ControlToValidate="txtSalary"></asp:RequiredFieldValidator>
        <li>Joining Date</li>

             <li>
        <%--<ew:calendarpopup ID="calJoiningDate" runat="server"></ew:calendarpopup>--%>
                  <asp:TextBox ID="CalJoiningDate1" runat="server" CssClass="text_input"></asp:TextBox>
                  <cc1:CalendarExtender ID="CalJoiningDate1_CalendarExtender" runat="server" 
                       Enabled="True" TargetControlID="CalJoiningDate1">
                  </cc1:CalendarExtender>
        </li>


    </ul>
    <ul>
        <li>Job Area</li>
        <li><asp:DropDownList runat="server" class="text_input" id="ddlJobArea"/></li>
        <li>Desigantion</li>
        <li><asp:DropDownList runat="server" class="text_input" id="ddlDesignation"/></li>
    </ul>

    <asp:UpdatePanel ID="updatePnl" runat="server">
    <ContentTemplate>    
    <div>
    <ul>
        <li style="margin-top:20px; margin-bottom:15px"><input type="image" src="images/img_jobHistory.png"  /></li>
    </ul>

    <ul>
    <li>Company</li>
   <%--<asp:DropDownList runat="server" class="text_input" id="ddlCompany" 
              onselectedindexchanged="ddlCompany_SelectedIndexChanged"/>--%>
          <li><asp:TextBox ID="CompanyNames" runat="server" CssClass="text_input"></asp:TextBox> 
               <cc1:AutoCompleteExtender ID="CompanyNames_AutoCompleteExtender" runat="server" 
                    DelimiterCharacters="" Enabled="True" ServicePath="getCompanies.asmx" ServiceMethod="getCompaniesMethod" MinimumPrefixLength="1" 
                    TargetControlID="CompanyNames" UseContextKey="True">
               </cc1:AutoCompleteExtender>
               <li>Job Duration</li>
               <li>
                    <asp:TextBox ID="txtJobDuration" runat="server" class="text_input" />
               </li>
               <asp:RequiredFieldValidator ID="validatorJobDuration" runat="server" 
                    ControlToValidate="txtJobDuration" ErrorMessage="*" 
                    ValidationGroup="jobHistory"></asp:RequiredFieldValidator>
         </li>
    </ul>

    <ul>
    <li>Job Area</li>
    <li><asp:DropDownList runat="server" class="text_input" id="ddlJobHistJobArea"/></li>
    <li>Designation</li>
    <li><asp:DropDownList runat="server" class="text_input" id="ddlJobHistoryDesignation"/></li>
    </ul>

    <ul>
    <li>Last Drawn Salary</li>
    <li><asp:TextBox runat="server" class="text_input" id="txtLastDrawnSalary"/>
         </li>
        <asp:RequiredFieldValidator ID="validatorLastDrawnSalary" runat="server" ValidationGroup="jobHistory" ErrorMessage="*" ControlToValidate="txtLastDrawnSalary"></asp:RequiredFieldValidator>
    </ul>

    <ul>
        <li style="margin-left:160px">
            <asp:ImageButton src="images/btn_Add.png" runat="server" ID="btnAddJobHistory" onclick="btnAddJobHistory_Click" CausesValidation="True" ValidationGroup="jobHistory"/>
            <asp:ImageButton src="images/btnReset.png" runat="server" ID="btnResetJH" CausesValidation="False" onclick="btnResetJH_Click"/>
        </li>
    </ul>

    <ul style="margin-left:30px">
    <asp:GridView ID="grdJobHistory" runat="server" AutoGenerateColumns="False" Width="798px" 
            AllowPaging="True" 
        onpageindexchanging="grdJobHistory_PageIndexChanging" 
        onrowcommand="grdJobHistory_RowCommand" 
            onrowdeleting="grdJobHistory_RowDeleting" 
            onrowediting="grdJobHistory_RowEditing" Font-Bold="True" 
            Font-Names="Calibri" Font-Size="Medium" ForeColor="White" PageSize="5">
            <Columns>
                <asp:BoundField DataField="EmployeeExperianceId" 
                    HeaderText="EmployeeExperianceId" />
                <asp:BoundField HeaderText="Company" DataField="CompanyName" ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField DataField="JobAreaName" HeaderText="Job Area" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Smaller" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Designation" DataField="DesignationName" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Experiance Duration" DataField="ExperianceDuration" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Salary" DataField="LastDrawnSalary" ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:TemplateField AccessibleHeaderText="Edit" HeaderText="Edit">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnEdit" runat="server" ImageUrl="images/updateIcon.png" CausesValidation="False" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeExperianceId") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField AccessibleHeaderText="Delete" HeaderText="Delete">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="images/deleteIcon.png" CausesValidation="False" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeExperianceId") %>' OnClientClick="return confirm('Are you sure you want to delete this record...');"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#5D92B9" BorderStyle="None" />
            <AlternatingRowStyle BackColor="#AAAAAA" />
        </asp:GridView>
    </ul>

    </div>

    <div>
    <ul>
        <li style="margin-top:20px; margin-bottom:15px"><input type="image" src="images/img_skillDetail.png"  /></li>
    </ul>

    <ul>
        <li>Skill</li>
        <!--<li><asp:DropDownList runat="server" class="text_input" id="ddlSkill"/></li>-->
        <li>
             <asp:TextBox ID="SkillAutoComplete" runat="server" CssClass="text_input"></asp:TextBox>
             <cc1:AutoCompleteExtender ID="SkillAutoComplete_AutoCompleteExtender" 
                  runat="server" DelimiterCharacters="" Enabled="True" ServicePath="getSkills.asmx" ServiceMethod="getAllSkills" MinimumPrefixLength="1" 
                  TargetControlID="SkillAutoComplete">
             </cc1:AutoCompleteExtender>
         </li>
        <li>Skill Detail</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtSkillDetail"/></li>
        <asp:RequiredFieldValidator ID="validatorSkillDetail" runat="server" ValidationGroup="skill" ErrorMessage="*" ControlToValidate="txtSkillDetail"></asp:RequiredFieldValidator>
    </ul>    

    <ul>
        <li style="margin-left:160px">
            <asp:ImageButton src="images/btn_Add.png" runat="server" ID="btnAddSkill" CausesValidation="True" ValidationGroup="skill" onclick="btnAddSkill_Click"/>
            <asp:ImageButton src="images/btnReset.png" runat="server" ID="btnResetSkill" CausesValidation="False" onclick="btnResetSkill_Click"/>
        </li>
    </ul>    

    <ul style="margin-left:30px">
    <asp:GridView ID="grdSkill" runat="server" AutoGenerateColumns="False" Width="798px" 
            AllowPaging="True" 
        onpageindexchanging="grdSkill_PageIndexChanging" 
        onrowcommand="grdSkill_RowCommand" 
            onrowdeleting="grdSkill_RowDeleting" 
            onrowediting="grdSkill_RowEditing" Font-Bold="True" 
            Font-Names="Calibri" Font-Size="Medium" ForeColor="White" PageSize="5">
            <Columns>
                <asp:BoundField HeaderText="EmployeeSkillId" DataField="EmployeeSkillId" >
                </asp:BoundField>
                <asp:BoundField DataField="SkillName" HeaderText="Skill Name" ReadOnly="True">
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Skill Detail" DataField="SkillDetail" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnEdit" runat="server" ImageUrl="images/updateIcon.png" CausesValidation="false" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeSkillId") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="images/deleteIcon.png" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeSkillId") %>' OnClientClick="return confirm('Are you sure you want to delete this record...');"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#5D92B9" BorderStyle="None" />
            <AlternatingRowStyle BackColor="#AAAAAA" />
        </asp:GridView>
    </ul>
    </div>

    <div>       
    <ul>
        <li style="margin-top:20px; margin-left:20px; margin-bottom:15px"><input type="image" src="images/img_qualificationDetail.png"  /></li>
    </ul>    

    <ul>
        <li>Course</li>
        <li><asp:DropDownList runat="server" class="text_input" id="ddlCourse"/></li>
        <li>Institution</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtInstitution"/></li>
        <asp:RequiredFieldValidator ID="validatorInsitution" runat="server" ValidationGroup="course" ErrorMessage="*" ControlToValidate="txtInstitution"></asp:RequiredFieldValidator>
    </ul>        

    <ul>
        <li>Duration</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtCourseDuration"/></li>
        <asp:RequiredFieldValidator ID="validatorDuration" runat="server" ValidationGroup="course" ErrorMessage="*" ControlToValidate="txtCourseDuration"></asp:RequiredFieldValidator>
        <li>Passing Year</li>
        <li>
             <asp:TextBox ID="calPassingYear1" runat="server" CssClass="text_input"></asp:TextBox>

        </li>
        <!--<li><ew:CalendarPopup ID="calPassingYear" runat="server"></ew:CalendarPopup></li>-->
    </ul>        

    <ul>
        <li>Marks/Percentage/GPA</li>
        <li><asp:TextBox runat="server" class="text_input" id="txtMarksPercentage"/></li>
        <asp:RequiredFieldValidator ID="validatorMarks" runat="server" ValidationGroup="course" ErrorMessage="*" ControlToValidate="txtMarksPercentage"></asp:RequiredFieldValidator>
        <li style="margin-left:160px">
        <asp:ImageButton src="images/btn_Add.png" runat="server" ID="btnAddCourse" CausesValidation="True" onclick="btnAddCourse_Click" ValidationGroup="course"/>
        <asp:ImageButton src="images/btnReset.png" runat="server" ID="btnResetCourse" CausesValidation="False" onclick="btnResetCourse_Click" />
        </li>
    </ul>        

    <ul style="margin-left:30px">
    <asp:GridView ID="grdCourse" runat="server" AutoGenerateColumns="False" Width="798px" 
            AllowPaging="True" 
        onpageindexchanging="grdCourse_PageIndexChanging" 
        onrowcommand="grdCourse_RowCommand" 
            onrowdeleting="grdCourse_RowDeleting" 
            onrowediting="grdCourse_RowEditing" Font-Bold="True" 
            Font-Names="Calibri" Font-Size="Medium" ForeColor="White" PageSize="5">
            <Columns>
                <asp:BoundField DataField="EmployeeQualificationId" 
                    HeaderText="EmployeeQualificationId" />
                <asp:BoundField HeaderText="Course" DataField="CourseTitle" ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Institution" DataField="InstitutionName" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Duration" DataField="Duration" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Passing Year" DataField="YearOfPassing" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Marks/Percentage/GPA" DataField="MarksPercentage" 
                    ReadOnly="True" >
                    <ItemStyle Font-Names="Calibri,Verdana,Arial,Helvetica,sans-serif" 
                        Font-Size="Small" ForeColor="#3F4A53" />
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnEdit" runat="server" ImageUrl="images/updateIcon.png" CausesValidation="false" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeQualificationId") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="images/deleteIcon.png" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "EmployeeQualificationId") %>' OnClientClick="return confirm('Are you sure you want to delete this record...');"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#5D92B9" BorderStyle="None" />
            <AlternatingRowStyle BackColor="#AAAAAA" />
        </asp:GridView>
    </ul>
    </div>
    </ContentTemplate>
      </asp:UpdatePanel>        
    <ul>
       <li style="margin-left:90px; margin-bottom:10px;">
       <asp:ImageButton ID="btnSubmit" AlternateText="submit button" runat="server" src="images/btn_Submit.png" onclick="btnSubmit_Click"/>
       </li>
    </ul>
</div>
            <!--CLOSE CONTENT DIV-->

</asp:Content>

例如:

当我尝试添加一些 ASP.NET 服务器端控件时,例如 <asp:testBox id="txtBox" runat="server"/>它们在后面的代码中不可访问。:

Error: txtBox is not defined in the current Context ...

我做错了什么?

最佳答案

当我通过在解决方案中创建一个 asp.net 应用程序项目并将 aspx 和 aspx.cs 文件复制过来,将网站移动到主要解决方案时,我遇到了这个问题。我通过右键单击项目并转换为 Web 应用程序来解决它。这创建了必要的 designer.cs 文件,其中包含 aspx 页面上的 asp 对象的代码。

关于c# - 无法在代码隐藏中访问 Asp.net 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180509/

相关文章:

c# - 提取字符串数据 - 从右侧?

asp.net - 如何向服务调用完成时调用的代码隐藏注册回调?

asp.net - 从 jQuery 调用 ASP.NET ASMX Web 服务

c# - 查找是否多次包含相同元素的字符串列表

c# - 使用属性来满足接口(interface)需求

C# 用正则表达式拆分 string.format 字符串

c# - 代码隐藏文件无法识别 aspx 控件

c# - 如何使网站只能从指定的IP地址打开

c# - 编译输出类型为 Exe 的 Windows 服务

c# - 无法建立连接,因为目标机器主动拒绝 127.0.0.1