无法将类型“System.Web.UI.WebControls.DropDownList”隐式转换为“DropDownList”

ai小虎 2015-06-11 10:19:40
protected void Button4_Click(object sender, EventArgs e)
{
DropDownList obj_ddl;
obj_ddl = DropDownList1;

System.Web.UI.WebControls.DropDownList ddl;
ddl = DropDownList1;



}
都是一样的声明,为什么第一种( DropDownList obj_ddl; obj_ddl = DropDownList1;)写法出现如下错误:
无法将类型“System.Web.UI.WebControls.DropDownList”隐式转换为“DropDownList”
而第二种写法( System.Web.UI.WebControls.DropDownList ddl; ddl = DropDownList1; )
却可以???求指教
...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2015-06-18
  • 打赏
  • 举报
回复
类型强转一下
Ajax实现无刷新三联动下拉框 1.html代码 Ajax实现无刷新三联动下拉框
省市 dropdownlist id="DropDownList1" runat="server">dropdownlist>
城市 dropdownlist id="DropDownList2" runat="server">dropdownlist>
市区 dropdownlist id="DropDownList3" runat="server">dropdownlist>
2.cs代码 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace AjaxTest { /**//// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.DropDownList DropDownList2; protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.DropDownList DropDownList3; private void Page_Load(object sender, System.EventArgs e) { Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod)); if(!Page.IsPostBack) { this.DropDownList1.DataSource=AjaxMethod.GetProvinceList(); this.DropDownList1.DataTextField="province"; this.DropDownList1.DataValueField="provinceID"; this.DropDownList1.DataBind(); this.DropDownList1.Attributes.Add("onclick","cityResult();"); this.DropDownList2.Attributes.Add("onclick","areaResult();"); } } Web Form Designer generated code#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /**//// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }3.AjaxMethod using System; using System.Data; using System.Data.SqlClient; namespace AjaxTest { /**//// /// Summary description for AjaxMethod. /// public class AjaxMethod { GetProvinceList#region GetProvinceList public static DataSet GetProvinceList() { string sql="select * from province"; return GetDataSet(sql); } #endregion GetCityList#region GetCityList [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)] public DataSet GetCityList(int provinceid) { string sql="select * from city where father="+provinceid; return GetDataSet(sql); } #endregion GetAreaList#region GetAreaList [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)] public DataSet GetAreaList(int cityid) { string sql="select * from area where father="+cityid; return GetDataSet(sql); } #endregion GetDataSet#region GetDataSet public static DataSet GetDataSet(string sql) { string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString); DataSet ds=new DataSet(); sda.Fill(ds); return ds; } #endregion } }4.web.config
实现城市连动 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace _0327Province_City_代码实现_ { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "province"; string selectstring = "select * from " + Tablename; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_Province .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_Province .DataTextField = "province"; this.ddl_Province .DataValueField = "provinceID"; this.ddl_Province .DataBind(); } } protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e) { if (this.ddl_City .SelectedIndex >= 0) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "area"; string selectstring = "select * from " + Tablename + " where cityid='" + this.ddl_City .SelectedValue + "'"; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_Area .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_Area .DataTextField = "area"; this.ddl_Area .DataValueField = "areaID"; this.ddl_Area .DataBind(); } } protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e) { if (this.ddl_Province .SelectedIndex >= 0) { string connstring = @"Data Source=.\SQLExpress;Initial Catalog=IDInfoDB;Integrated Security=True";//@表示转义字符 .\sqlexpress表示数据源 SqlConnection sconn = new SqlConnection(connstring); string Tablename = "city"; string selectstring = "select * from " + Tablename + " where provinceid='" + this.ddl_Province .SelectedValue + "'"; SqlDataAdapter sda = new SqlDataAdapter(selectstring, sconn); DataSet ds = new DataSet(); sconn.Open(); sda.Fill(ds, Tablename); sconn.Close(); this.ddl_City .DataSource = ds.Tables[Tablename].DefaultView; this.ddl_City .DataTextField = "city"; this.ddl_City .DataValueField = "cityID"; this.ddl_City .DataBind(); } } } }

28,397

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧