请大家帮我看看吧,不知道为什么会有这样的报错:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="公路级别:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:Label ID="Label2" runat="server" Text="设计速度"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</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;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
static string daoludengji = "高速";
string strCon = "Data Source=7-PC;Initial Catalog=caebon;Integrated Security=True;Pooling=False";
string strQuery1 = "SELECT 道路等级 FROM LevelOfRoad GROUP BY 道路等级 ORDER BY CASE 道路等级 when' 高速' then 1 when '一级' then 2 when '二级' then 3 when '三级' then 4 when'四级'then 5 end ";
string strQuery2 = string.Format("SELECT 设计速度 FROM LevelOfRoad WHERE 道路等级='{0}' GROUP BY 设计速度 ORDER BY 设计速度 ",daoludengji );
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
SqlConnection myConnection = new SqlConnection(strCon);
SqlDataAdapter myAdapter1 = new SqlDataAdapter(strQuery1, myConnection);
DataSet myDataSet1 = new DataSet();
myAdapter1.Fill(myDataSet1, "LevelOfRoad");
DropDownList1.DataSource = myDataSet1;
DropDownList1.DataValueField = "道路等级";
DropDownList1.DataTextField = "道路等级";
DropDownList1.DataBind();
daoludengji = DropDownList1.SelectedItem.Text.ToString();
SqlDataAdapter myAdapter2 = new SqlDataAdapter(strQuery2, myConnection);
DataSet myDataSet2 = new DataSet();
myAdapter2.Fill(myDataSet2, "LevelOfRoad");
DropDownList2.DataSource = myDataSet2;
DropDownList2.DataValueField = "设计速度";
DropDownList2.DataTextField = "设计速度";
DropDownList2.DataBind();
myConnection.Close();
myConnection.Close();
}
}
}报错为:
当前上下文不存在名称:“DropDownList2”
为什么呢?我明明 DropDownList2控件呀!
把DropDownList1注释掉、给daoludengji一个初值以后,仍然是同样的报错。
求高手赐教!在线等!!!
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="公路级别:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:Label ID="Label2" runat="server" Text="设计速度"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</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;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
static string daoludengji = "高速";
string strCon = "Data Source=7-PC;Initial Catalog=caebon;Integrated Security=True;Pooling=False";
string strQuery1 = "SELECT 道路等级 FROM LevelOfRoad GROUP BY 道路等级 ORDER BY CASE 道路等级 when' 高速' then 1 when '一级' then 2 when '二级' then 3 when '三级' then 4 when'四级'then 5 end ";
string strQuery2 = string.Format("SELECT 设计速度 FROM LevelOfRoad WHERE 道路等级='{0}' GROUP BY 设计速度 ORDER BY 设计速度 ",daoludengji );
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
SqlConnection myConnection = new SqlConnection(strCon);
SqlDataAdapter myAdapter1 = new SqlDataAdapter(strQuery1, myConnection);
DataSet myDataSet1 = new DataSet();
myAdapter1.Fill(myDataSet1, "LevelOfRoad");
DropDownList1.DataSource = myDataSet1;
DropDownList1.DataValueField = "道路等级";
DropDownList1.DataTextField = "道路等级";
DropDownList1.DataBind();
daoludengji = DropDownList1.SelectedItem.Text.ToString();
SqlDataAdapter myAdapter2 = new SqlDataAdapter(strQuery2, myConnection);
DataSet myDataSet2 = new DataSet();
myAdapter2.Fill(myDataSet2, "LevelOfRoad");
DropDownList2.DataSource = myDataSet2;
DropDownList2.DataValueField = "设计速度";
DropDownList2.DataTextField = "设计速度";
DropDownList2.DataBind();
myConnection.Close();
myConnection.Close();
}
}
}报错为:
当前上下文不存在名称:“DropDownList2”
为什么呢?我明明 DropDownList2控件呀!
把DropDownList1注释掉、给daoludengji一个初值以后,仍然是同样的报错。
求高手赐教!在线等!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货