ASP.net & C# - 当前上下文中不存在控件,designer.cs 不存在
ASP.net & C# - The Control does not exist in the current context, designer.cs doesn't exist
这与我正在从事的项目有关。我的表格几乎都放在一起了。每个下拉列表都将正确填充并相互馈送。现在我只需要获取表单以将插入查询发送回数据库。当我简单地单击数据库中的 "Insert" 按钮时,我收到一条错误消息,指出“无法将值 NULL 插入列 'Issue_ID'、table 'NintendoPowerPoll.dbo.Poll_Results';列不允许空值。INSERT 失败。
声明已终止。"
我已经发现这意味着每个字段的选定值没有被输入到查询中。因此,我开始尝试在 *.asp.cs 文件中为“插入”按钮设置代码,这样站点将 运行 插入查询,从所选字段中提取必要的信息。但是,当我尝试让查询从 IssueDateDropDownList 提供 .SelectedItem.Value 时,Visual Studio 2013 在控件名称下给出了一条错误消息,该消息显示为 "The Name IssueDateDropDownList does not exist in the current context." 这里的进一步研究表明这是 Designer.cs 文件的问题...只是该文件在解决方案资源管理器的项目文件列表中不可见。我还能做些什么来解决这个问题吗?
ASP 站点:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultsEntryForm.aspx.cs" Inherits="ResultsEntryForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="ResultsDataEntryForm" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="PollResultsDataSource"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="Issue_Date" HeaderText="Issue_Date"
SortExpression="Issue_Date" />
<asp:BoundField DataField="Platform_Type" HeaderText="Platform_Type"
SortExpression="Platform_Type" />
<asp:BoundField DataField="Element_Title" HeaderText="Element_Title"
SortExpression="Element_Title" />
<asp:BoundField DataField="Poll_Score" HeaderText="Poll_Score"
SortExpression="Poll_Score" />
</Columns>
</asp:GridView>
<asp:FormView ID="ResultsFormView" runat="server" AllowPaging="True"
DataSourceID="PollResultsDataSource">
<InsertItemTemplate>
Issue_Date:
<asp:DropDownList ID="IssueDateDropDownList" runat="server" AutoPostBack="True"
DataSourceID="IssueDateDropDownDataSource" DataTextField="Issue_Date"
DataValueField="Issue_ID"
SelectedValue='<%# Bind("Issue_Date", "{0:d}") %>'>
</asp:DropDownList>
<br />
Platform_Type:
<asp:DropDownList ID="PlatformDropDownList" runat="server"
DataSourceID="PlatformDropDownDataSource" DataTextField="Platform_Type"
DataValueField="Platform_ID" AutoPostBack="True">
</asp:DropDownList>
<br />
Element_Title:
<asp:DropDownList ID="TitleDropDownList" runat="server" AutoPostBack="True"
DataSourceID="GameSqlDataSource" DataTextField="Element_Title"
DataValueField="Element_Group_ID">
</asp:DropDownList>
<br />
Poll_Score:
<asp:TextBox ID="ScoreTextBox" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" ValidationGroup="Insert"
OnClick="InsertButton_Click" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:SqlDataSource ID="GameSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="PlatformDropDownList" Name="Platform_ID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
<ItemTemplate>
Issue_Date:
<asp:Label ID="Issue_DateLabel" runat="server"
Text='<%# Bind("Issue_Date") %>' />
<br />
Platform_Type:
<asp:Label ID="Platform_TypeLabel" runat="server"
Text='<%# Bind("Platform_Type") %>' />
<br />
Element_Title:
<asp:Label ID="Element_TitleLabel" runat="server"
Text='<%# Bind("Element_Title") %>' />
<br />
Poll_Score:
<asp:Label ID="Poll_ScoreLabel" runat="server"
Text='<%# Bind("Poll_Score") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="PollResultsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
InsertCommand="INSERT INTO Poll_Results(Issue_ID, Element_Group_ID, Poll_Score) VALUES (@Issue_ID, @Element_Group_ID, @Poll_Score)"
SelectCommand="SELECT NintendoPowerIssue.Issue_Date, na_lkpPlatformTypes.Platform_Type, na_Games.Element_Title, Poll_Results.Poll_Score
FROM Poll_Results INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN na_lkpPlatformTypes ON na_Games.Platform_ID = na_lkpPlatformTypes.Platform_ID"
UpdateCommand="UPDATE Poll_Results SET Poll_Score = @Poll_Score
FROM Poll_Results
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
WHERE (Poll_Results.Issue_ID = @Issue_ID) AND (Poll_Results.Element_Group_ID = @Element_Group_ID)">
<InsertParameters>
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
<asp:Parameter Name="Poll_Score" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Poll_Score" />
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="IssueDateDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Issue_ID], [Issue_Date] FROM [NintendoPowerIssue]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="PlatformDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Platform_ID], [Platform_Type] FROM [na_lkpPlatformTypes] WHERE [Platform_ID] IN (SELECT DISTINCT [Platform_ID] FROM [na_Games])" />
</form>
</body>
</html>
C# 后页:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class ResultsEntryForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private DataTable BindDropDownList(string field)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
try
{
connection.Open();
string sqlStatement = "SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@Platform_ID", field);
sqlDa.Fill(dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
Server.ClearError();
Response.Write(ex.Message + ("<br />") + ex.Source);
}
finally
{
connection.Close();
}
return dt;
}
protected void InsertButton_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
Int32 IssueID = Convert.ToInt32(IssueDateDropDownList.SelectedItem.Value);
try
{
connection.Open();
}
}
}
我需要删除 BindDropDownList 代码,因为它不需要,但我认为这与手头的问题无关。
如果代码最初是用旧版本的 VS 编写的,可能是因为 VS 2013 在不同的地方期望的东西。
看这里:The name 'control' does not exist in the current context
此外 ASP WebForms 没有 designer.cs class。但是每个页面(aspx class)下面都有一个 *.aspx.cs 文件和一个 *.aspx.designer.cs 文件。 (其中 x)是页面的名称。如果它们不存在,则说明您的项目结构有问题。
所以,我找出了导致问题的原因。因为有问题的下拉框位于表单模板中(特别是在 FormView 的插入模板下),所以有问题的脚本无法 "see" 有问题的下拉列表。当我在表单视图之外重新创建插入表单时,它能够 "see" 必要的控件。问题不在于 C# 代码,而在于网站前端的结构。
这与我正在从事的项目有关
我已经发现这意味着每个字段的选定值没有被输入到查询中。因此,我开始尝试在 *.asp.cs 文件中为“插入”按钮设置代码,这样站点将 运行 插入查询,从所选字段中提取必要的信息。但是,当我尝试让查询从 IssueDateDropDownList 提供 .SelectedItem.Value 时,Visual Studio 2013 在控件名称下给出了一条错误消息,该消息显示为 "The Name IssueDateDropDownList does not exist in the current context." 这里的进一步研究表明这是 Designer.cs 文件的问题...只是该文件在解决方案资源管理器的项目文件列表中不可见。我还能做些什么来解决这个问题吗?
ASP 站点:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultsEntryForm.aspx.cs" Inherits="ResultsEntryForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="ResultsDataEntryForm" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="PollResultsDataSource"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="Issue_Date" HeaderText="Issue_Date"
SortExpression="Issue_Date" />
<asp:BoundField DataField="Platform_Type" HeaderText="Platform_Type"
SortExpression="Platform_Type" />
<asp:BoundField DataField="Element_Title" HeaderText="Element_Title"
SortExpression="Element_Title" />
<asp:BoundField DataField="Poll_Score" HeaderText="Poll_Score"
SortExpression="Poll_Score" />
</Columns>
</asp:GridView>
<asp:FormView ID="ResultsFormView" runat="server" AllowPaging="True"
DataSourceID="PollResultsDataSource">
<InsertItemTemplate>
Issue_Date:
<asp:DropDownList ID="IssueDateDropDownList" runat="server" AutoPostBack="True"
DataSourceID="IssueDateDropDownDataSource" DataTextField="Issue_Date"
DataValueField="Issue_ID"
SelectedValue='<%# Bind("Issue_Date", "{0:d}") %>'>
</asp:DropDownList>
<br />
Platform_Type:
<asp:DropDownList ID="PlatformDropDownList" runat="server"
DataSourceID="PlatformDropDownDataSource" DataTextField="Platform_Type"
DataValueField="Platform_ID" AutoPostBack="True">
</asp:DropDownList>
<br />
Element_Title:
<asp:DropDownList ID="TitleDropDownList" runat="server" AutoPostBack="True"
DataSourceID="GameSqlDataSource" DataTextField="Element_Title"
DataValueField="Element_Group_ID">
</asp:DropDownList>
<br />
Poll_Score:
<asp:TextBox ID="ScoreTextBox" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" ValidationGroup="Insert"
OnClick="InsertButton_Click" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:SqlDataSource ID="GameSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="PlatformDropDownList" Name="Platform_ID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
<ItemTemplate>
Issue_Date:
<asp:Label ID="Issue_DateLabel" runat="server"
Text='<%# Bind("Issue_Date") %>' />
<br />
Platform_Type:
<asp:Label ID="Platform_TypeLabel" runat="server"
Text='<%# Bind("Platform_Type") %>' />
<br />
Element_Title:
<asp:Label ID="Element_TitleLabel" runat="server"
Text='<%# Bind("Element_Title") %>' />
<br />
Poll_Score:
<asp:Label ID="Poll_ScoreLabel" runat="server"
Text='<%# Bind("Poll_Score") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="PollResultsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
InsertCommand="INSERT INTO Poll_Results(Issue_ID, Element_Group_ID, Poll_Score) VALUES (@Issue_ID, @Element_Group_ID, @Poll_Score)"
SelectCommand="SELECT NintendoPowerIssue.Issue_Date, na_lkpPlatformTypes.Platform_Type, na_Games.Element_Title, Poll_Results.Poll_Score
FROM Poll_Results INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN na_lkpPlatformTypes ON na_Games.Platform_ID = na_lkpPlatformTypes.Platform_ID"
UpdateCommand="UPDATE Poll_Results SET Poll_Score = @Poll_Score
FROM Poll_Results
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
WHERE (Poll_Results.Issue_ID = @Issue_ID) AND (Poll_Results.Element_Group_ID = @Element_Group_ID)">
<InsertParameters>
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
<asp:Parameter Name="Poll_Score" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Poll_Score" />
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="IssueDateDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Issue_ID], [Issue_Date] FROM [NintendoPowerIssue]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="PlatformDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Platform_ID], [Platform_Type] FROM [na_lkpPlatformTypes] WHERE [Platform_ID] IN (SELECT DISTINCT [Platform_ID] FROM [na_Games])" />
</form>
</body>
</html>
C# 后页:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class ResultsEntryForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private DataTable BindDropDownList(string field)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
try
{
connection.Open();
string sqlStatement = "SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@Platform_ID", field);
sqlDa.Fill(dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
Server.ClearError();
Response.Write(ex.Message + ("<br />") + ex.Source);
}
finally
{
connection.Close();
}
return dt;
}
protected void InsertButton_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NintendoPowerPollConnectionString"].ToString());
Int32 IssueID = Convert.ToInt32(IssueDateDropDownList.SelectedItem.Value);
try
{
connection.Open();
}
}
}
我需要删除 BindDropDownList 代码,因为它不需要,但我认为这与手头的问题无关。
如果代码最初是用旧版本的 VS 编写的,可能是因为 VS 2013 在不同的地方期望的东西。
看这里:The name 'control' does not exist in the current context
此外 ASP WebForms 没有 designer.cs class。但是每个页面(aspx class)下面都有一个 *.aspx.cs 文件和一个 *.aspx.designer.cs 文件。 (其中 x)是页面的名称。如果它们不存在,则说明您的项目结构有问题。
所以,我找出了导致问题的原因。因为有问题的下拉框位于表单模板中(特别是在 FormView 的插入模板下),所以有问题的脚本无法 "see" 有问题的下拉列表。当我在表单视图之外重新创建插入表单时,它能够 "see" 必要的控件。问题不在于 C# 代码,而在于网站前端的结构。