c# - 代码隐藏中无法识别新控件

标签 c# asp.net visual-studio-2010 visual-studio-2013

我在我的 ASPX 页面中手动添加了几个字段,该页面实际上包含一个 FormView。手动,我的意思是直接在 Markup 编辑器中键入代码。

现在,当我尝试在后面的代码中访问这些控件时,智能感知中什么也没有出现。

我删除了designer.cs文件,右键点击ASPX页面,选择Convert to web application,还是不行。

设计器文件已经正确定义了 FormView 控件,但没有关于它的子控件的信息。

我真的需要使用 FindControl 来让它工作吗?如果您需要发布一些代码,请告诉我,因为目前我可能只是对 ASP.NET 实际工作(或此时不工作)有点困惑。

这是标记代码:

<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="WebForm_PatientForm.aspx.cs" Inherits="WebAppWalkthrough.WebForm_PatientForm" Title="Employee form" %>
<%@ MasterType VirtualPath="~/Site.Master" %>
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <asp:FormView ID="PatientForm" runat="server" DefaultMode="Edit">
        <EditItemTemplate>
            <table>
                <tr>
                    <td class="FormViewHeader">
                        Patient Name:
                    </td>
                    <td>
                        <asp:TextBox ID="PatientName" ReadOnly="true" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Patient ID:
                    </td>
                    <td>
                        <asp:TextBox ID="PatientID" ReadOnly="true" runat="server" Text='<%# Bind("patientid") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Eligibility
                    </td>
                    <td>
                        <asp:DropDownList runat="server" ID="Eligibility" SelectedValue='<%# Bind("service_executive_eligibility") %>' >
                            <asp:ListItem Value="" Text="N/A" />
                            <asp:ListItem Value="True" Text="Yes" />
                            <asp:ListItem Value="False" Text="No" />
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
            <asp:LinkButton ID="LinkButton2" runat="server"  OnCommand="UpdatePatientInfo" Text="UpdateInfo" CommandArgument='<%# Bind("patientid") %>'>Update</asp:LinkButton>
            <asp:LinkButton ID="LinkButton3" runat="server" OnCommand="CancelEditPatient" CommandName="CancelUpdate" Text="CancelInfo">Cancel</asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
        <table>
                <tr>
                    <td class="FormViewHeader">
                        Employee First Name:
                    </td>
                    <td>
                        <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Employee Last Name:
                    </td>
                    <td>
                        <asp:TextBox ID="LastName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Email address:
                    </td>
                    <td>
                        <asp:TextBox ID="EmailAddress" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Work phone number:
                    </td>
                    <td>
                        <asp:TextBox ID="PhoneNumber" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="FormViewHeader">
                        Eligibility
                    </td>
                    <td>
                        <asp:DropDownList runat="server" ID="Eligibility">
                            <asp:ListItem Value="True" Text="Yes" />
                            <asp:ListItem Value="False" Text="No" />
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
            <asp:LinkButton ID="LinkButton5" runat="server"  OnCommand="AddEmployee" Text="Add Employee">Add</asp:LinkButton>
        </InsertItemTemplate>
    </asp:FormView>
</asp:Content>

即使我在设计器文件中手动添加控件,我在运行时的代码后面也会出现空引用错误(即使 intelisense 获取 FirstName 对象在运行时为空)。

编辑:我将尝试安装 VS2010 SP1 和 this hotfix我找到并发布了结果。

编辑 2:所以我安装了 VS2010 SP1,但没有解决问题。我无法安装修补程序,因为我的系统是 x64。

所以我放弃了 VS2010 并尝试使用 VS2013,它仍然没有选择标记控件。

即使 Page.FindControl("ControlName") 也返回 null。该网页或 VS2013 的内容非常糟糕...

谢谢。

最佳答案

我遇到了一个问题,这个问题似乎与之前用于 WebForms 的 Telerik UI 组件类似。我想访问在网格中的模板内定义的项目,但不幸的是,唯一的方法是深入该网格的子元素。 designer.cs 文件中也不存在内部元素。 我不确定,但这可能是 WebForms 中嵌套元素的常见行为。

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

相关文章:

ASP.Net 日历/事件应用程序

c++ - OpenCV 2.3 和 Visual Studio 2010

c# - 在 Visual Studio 中使用引用调试项目

c# - 为什么要占用大约 10mb 空间的空 Xamarin Android 应用程序?

c# - 什么时候使用 PreRender 而不是 PageLoad?

c# - 嵌套的 lambda 语法

asp.net - <modules runAllManagedModulesForAllRequests ="true"/> 含义

.net - 安装包时 NuGet 添加引用错误

c# - 如何在不覆盖当前数据的情况下将数据写入文本文件

c# - 哪个胜过另一个?(与字符串相关)