日期:2014-05-19  浏览次数:20389 次

MagicAjax+DropDownList联动的取值问题,总是默认值,郁闷啊,盼高手高高手解决!!
页面就四个控件:     两个联动DropDownList,一个Label,一个Button代码很简单,第一次用这个玩意,出错了,请帮忙看看.

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
                  //   在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
SqlConnection   conn   =   new   SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "ConnectionString "]);//定义连接对象并指定连接字符串
SqlCommand   cmd   =   new   SqlCommand( "select   *   from   sort   where   layer=0   order   by   id   desc ",   conn);
conn.Open();
SqlDataReader   dr=cmd.ExecuteReader();
ddlistparent.DataSource=dr;
ddlistparent.DataTextField= "powername ";
ddlistparent.DataValueField= "id ";
ddlistparent.DataBind();
dr.Close();
conn.Close();
ddlistparent.Items.Add(new   ListItem( "全部 ", "0 "));
ddlistparent.Items.FindByText( "全部 ").Selected=true;
ddlistchild.Items.Add(new   ListItem( "全部 ", "0 "));
}
}

#region   Web   窗体设计器生成的代码
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.ddlistparent.SelectedIndexChanged   +=   new   System.EventHandler(this.ddlistparent_SelectedIndexChanged);
this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);
this.Load   +=   new   System.EventHandler(this.Page_Load);

}
#endregion

private   void   ddlistparent_SelectedIndexChanged(object   sender,   System.EventArgs   e)
{
SqlConnection   conn   =   new   SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "ConnectionString "]);//定义连接对象并指定连接字符串
SqlCommand   cmd   =   new   SqlCommand( "select   *   from   sort   where   parentid= "+ddlistparent.SelectedValue,   conn);
conn.Open();
SqlDataReader   dr=cmd.ExecuteReader();
ddlistchild.DataSource=dr;
ddlistchild.DataTextField= "powername ";
ddlistchild.DataValueField= "id ";
ddlistchild.DataBind();
dr.Close();
conn.Close();
}

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
Label1.Text=ddlistchild.SelectedItem.Text;
}

------解决方案--------------------
ddlistparent 的autopostback设置为true了吗?
------解决方案--------------------
用ddlistchild的事件取值不就得了
------解决方案--------------------
我试了一下,没有楼主说的问题,缓存?
------解决方案--------------------
在执行ddlistparent_SelectedIndexChanged时 ddlistchild的绑定有没有变化,ddlistchild列表中的是不是 预期的值?

------解决方案--------------------
都用js控制,